Skip to content

Instantly share code, notes, and snippets.

@zainfathoni
Created April 10, 2017 07:09
Show Gist options
  • Select an option

  • Save zainfathoni/21d050c15d7da988c2c3d0b5d4bf7ee4 to your computer and use it in GitHub Desktop.

Select an option

Save zainfathoni/21d050c15d7da988c2c3d0b5d4bf7ee4 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 {
a, b, n := 0, 1, 0 // a & b are the future values
return func() int {
n, a, b = a, b, a+b
return n
}
}
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