Skip to content

Instantly share code, notes, and snippets.

@aalvesjr
Created November 15, 2015 04:23
Show Gist options
  • Save aalvesjr/47f91aa0e667e25c49cc to your computer and use it in GitHub Desktop.
Save aalvesjr/47f91aa0e667e25c49cc to your computer and use it in GitHub Desktop.
Fibonacci closure
package main
import "fmt"
func fib() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return a
}
}
func main() {
f := fib()
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