12

I'm reading 'CreateSpace An Introduction to Programming in Go 2012'

and on page 86 I found this evil magic

func makeEvenGenerator() func() uint {
    i := uint(0)

    return func() (ret uint) {
        ret = i
        i += 2
        return
    }
}

// here's how it's called
nextEven := makeEvenGenerator()
fmt.Println(nextEven())
fmt.Println(nextEven())
fmt.Println(nextEven())

1) Why is i not resetting ? 2) is nextEven() returning and uint or is Println so smart that it can work with everything ?

5
  • 2
    2) Yes, it's returning an uint. See the documentation on named result parameters.
    – Michael
    Nov 13, 2013 at 15:20
  • i was asking because makeEvenGenerator() is returning a function pointer (if it's called like that)
    – waaadim
    Nov 13, 2013 at 15:25
  • 1
    a play of the relevant code.
    – J. Holmes
    Nov 13, 2013 at 15:26
  • 1
    nextEven is an variable with the type func() uint, so nextEven() return an uint.
    – nvcnvn
    Nov 13, 2013 at 15:28
  • 1
    @waaadim Functions are values in Go, so makeEvenGenerator returns a function value, not a pointer. Therefore the right term would've been 'function' or 'function value' but definitely not function pointer.
    – nemo
    Nov 13, 2013 at 16:25

3 Answers 3

15

For the sake of clarity, I'll assign names to both functions:

func makeEvenGenerator() func() uint { // call this "the factory"
    i := uint(0)

    return func() (ret uint) { // call this "the closure"
        ret = i
        i += 2
        return
    }
}

The factory returns the closure – functions are first class citizens in Go i.e. they can be right-hand expressions, for example:

f := func() { fmt.Println("f was called"); }

f() // prints "f was called"

In your code, the closure wraps over the context of the factory, this is called lexical scoping. This is why the variable i is available inside the closure, not as a copy but as a reference to i itself.

The closure uses a named return value called ret. What this means is that inside the closure you'll have implicitly declared ret and at the point of return, whatever value ret has will be returned.

This line:

ret = i

will assign the current value of i to ret. It will not change i. However, this line:

i += 2

will change the value of i for the next time the closure is called.


Here you'll find a little closure example I wrote together for you. It's not extremely useful but illustrates the scope, purpose and use of closures pretty well in my opinion:

package main

import "fmt"

func makeIterator(s []string) func() func() string {
    i := 0
    return func() func() string {
        if i == len(s) {
            return nil
        }
        j := i
        i++
        return func() string {
            return s[j]
        }
    }
}

func main() {

    i := makeIterator([]string{"hello", "world", "this", "is", "dog"})

    for c := i(); c != nil; c = i() {
        fmt.Println(c())
    }

}
2
  • 1
    it looks like a weird class with one method. the factory thing is called only once, like a constructor. and then only the closure is getting called. Can ever then need arise to create a closure with arguments ? or is this it's "final state" ?
    – waaadim
    Nov 13, 2013 at 16:35
  • 1
    If you find this interesting I recommend reading into functional programming. The concept of functions as first-class-citizens comes from there.
    – thwd
    Nov 13, 2013 at 17:13
8

1) Why is i not resetting ?

Closures in Go capture variables by reference. That means the inner function holds a reference to the i variable in the outer scope, and each call of it accesses this same variable.

2) is nextEven() returning and uint or is Println so smart that it can work with everything ?

fmt.Println() (along with fmt.Print(), fmt.Fprint(), etc.) can work most types. It prints its arguments in the "default format". It is the same thing that is printed using fmt.Printf() using the %v verb.

-1

The variable in closure is free from neither code segment nor context.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.