Skip to content

Instantly share code, notes, and snippets.

@beala
Created March 30, 2012 15:06
Show Gist options
  • Save beala/2252161 to your computer and use it in GitHub Desktop.
Save beala/2252161 to your computer and use it in GitHub Desktop.
Solution for the fib exercise on the Go language tour.
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
last := 0
cur := 1
count := 0
return func() int {
switch count {
case 0:
count += 1
return last
case 1:
count += 1
return cur
}
cur, last = last+cur, cur
return cur
}
}
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