Skip to content

Instantly share code, notes, and snippets.

@raypereda
Created April 2, 2014 05:01
Show Gist options
  • Save raypereda/9928213 to your computer and use it in GitHub Desktop.
Save raypereda/9928213 to your computer and use it in GitHub Desktop.
fibonacci in go
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func(int) int {
return func(n int) int {
a, b := 0, 1
for i := 0; i < n; i++ {
a, b = b, a + b
}
return a
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f(i))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment