Skip to content

Instantly share code, notes, and snippets.

@andramarkov
Forked from weppos/44.go
Created February 20, 2018 04:04
Show Gist options
  • Save andramarkov/8cc6c4ab8afb07d59f7baf173d1a3560 to your computer and use it in GitHub Desktop.
Save andramarkov/8cc6c4ab8afb07d59f7baf173d1a3560 to your computer and use it in GitHub Desktop.
A Tour of Go Exercise: Fibonacci closure
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
f2, f1 := 0, 1
return func() int {
f := f2
f2, f1 = f1, f+f1
return f
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment