Skip to content

Instantly share code, notes, and snippets.

@rustamli
Last active May 8, 2016 01:08
Show Gist options
  • Save rustamli/0b06de4ec79f72be855d84dd3e5b876a to your computer and use it in GitHub Desktop.
Save rustamli/0b06de4ec79f72be855d84dd3e5b876a to your computer and use it in GitHub Desktop.
Fibo Closure Function in Go
package main
import "fmt"
func fibonacci() func() int {
beforeLast, last := 0, -1
return func () int {
if last < 1 {
last += 1
} else {
newLast := beforeLast + last
beforeLast = last
last = newLast
}
return last
}
}
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