box-o-sand/gotime/gotour-artifacts/exercise-fibonacci-closure/main.go

24 lines
330 B
Go
Raw Normal View History

package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
2012-11-06 02:00:35 +00:00
prev := 0
cur := 1
return func() int {
old_cur := cur
cur = prev + cur
prev = old_cur
return cur
}
}
func main() {
2012-11-06 02:00:35 +00:00
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}