Skip to content

Instantly share code, notes, and snippets.

@sivan
Last active December 3, 2019 18:40
Show Gist options
  • Save sivan/a08c112b38fee14826aaaa410357ae82 to your computer and use it in GitHub Desktop.
Save sivan/a08c112b38fee14826aaaa410357ae82 to your computer and use it in GitHub Desktop.
Go Exercise: Fibonacci closure
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
num, add := 1, -1
return func() int {
num, add = num+add, num
return num
}
}
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