Skip to content

Instantly share code, notes, and snippets.

@youchen
Created December 12, 2017 21:16
Show Gist options
  • Save youchen/2459e8e5fdb48aa092cb7759cfc97c6d to your computer and use it in GitHub Desktop.
Save youchen/2459e8e5fdb48aa092cb7759cfc97c6d to your computer and use it in GitHub Desktop.
package main
import "fmt"
func fibonacci() func() int {
fib1, fib2 := 0, 1
return func() int{
defer func() { fib1, fib2 = fib2, fib1 + fib2 } ()
return fib1
}
}
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