github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/doc/play/fib.go (about)

     1  package main
     2  
     3  import "fmt"
     4  
     5  // fib returns a function that returns
     6  // successive Fibonacci numbers.
     7  func fib() func() int {
     8  	a, b := 0, 1
     9  	return func() int {
    10  		a, b = b, a+b
    11  		return a
    12  	}
    13  }
    14  
    15  func main() {
    16  	f := fib()
    17  	// Function calls are evaluated left-to-right.
    18  	fmt.Println(f(), f(), f(), f(), f())
    19  }