Skip to content

Instantly share code, notes, and snippets.

@praveenkumar
Created April 3, 2014 04:44
Show Gist options
  • Save praveenkumar/9948395 to your computer and use it in GitHub Desktop.
Save praveenkumar/9948395 to your computer and use it in GitHub Desktop.
Exercise: Fibonacci closure
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
pre := 0
post := 1
return func() int{
sum := pre + post
pre, post = post, sum
return sum
}
}
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