Skip to content

Instantly share code, notes, and snippets.

@dallarosa
Last active October 11, 2015 13:37
Show Gist options
  • Save dallarosa/3866890 to your computer and use it in GitHub Desktop.
Save dallarosa/3866890 to your computer and use it in GitHub Desktop.
Fibonnaci closure
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
n := []int{0,1}
return func() int {
value := n[0]
temp := n[0]+n[1]
n[0] = n[1]
n[1] = temp
return value
}
}
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