Skip to content

Instantly share code, notes, and snippets.

@YOwatari
Last active November 18, 2015 04:51
Show Gist options
  • Save YOwatari/2f2663c1169df019c062 to your computer and use it in GitHub Desktop.
Save YOwatari/2f2663c1169df019c062 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 {
n1, n2 := 0, 1
return func() int {
n := n1 + n2
n1, n2 = n2, n
return n
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
if i < 2 {
fmt.Println(i)
}
fmt.Println(f())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment