Skip to content

Instantly share code, notes, and snippets.

@meltedice
Created October 2, 2017 05:52
Show Gist options
  • Save meltedice/dfec56a39106705da825d41db420e2a4 to your computer and use it in GitHub Desktop.
Save meltedice/dfec56a39106705da825d41db420e2a4 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 {
f0 := 0
f1 := 1
return func() int {
f := f0
f0 += f1
f1 = f
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