1 package main 2 3 import "fmt" 4 5 // fibonacci is a function that returns 6 // a function that returns an int. 7 func fibonacci() func() int { 8 pre := 0 9 cur := 0 10 return func() int { 11 if pre == 0 && cur == 0 { 12 pre = 1 13 } else { 14 temp := cur 15 cur += pre 16 pre = temp 17 } 18 19 return cur 20 } 21 } 22 23 func main() { 24 f := fibonacci() 25 for i := 0; i < 10; i++ { 26 fmt.Println(f()) 27 } 28 }