Skip to content

Instantly share code, notes, and snippets.

@karlmutch
Created July 14, 2015 22:27
Show Gist options
  • Save karlmutch/0c97833db93bcfcff132 to your computer and use it in GitHub Desktop.
Save karlmutch/0c97833db93bcfcff132 to your computer and use it in GitHub Desktop.
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
accum1 := 1
accum2 := 0
return func() int {
accum := accum1 + accum2
accum1 = accum2
accum2 = accum
return accum
}
}
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