public
Authored by
PotatoGim

[Go] Practice: Fibonacci Closure
Editedpackage main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
x, y, fib := 0, 1, 0
return func() int {
x, y, fib = y, fib, y+fib
return fib
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
Please register or sign in to comment