Skip to content

Instantly share code, notes, and snippets.

@sighmin
Last active August 29, 2015 13:56
Show Gist options
  • Save sighmin/9175834 to your computer and use it in GitHub Desktop.
Save sighmin/9175834 to your computer and use it in GitHub Desktop.
Go tour "Fibonacci closure factory" method: http://tour.golang.org
package main
import "fmt"
// Fibonacci closure factory
func fibonacci() func() int {
previous := 0
current := 1
return func() int {
result := previous
previous = current
current = result + current
return result
}
}
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