1. 程式人生 > >golang函數閉包實現波費納契數列

golang函數閉包實現波費納契數列

acc sta func art spa ret () main fibonacci

直接看代碼:

package main

import "fmt"

func fibonacci(s1, s2 int) func() int {
fmt.Println("start with:", s1, s2)
left := s1
right := s2
return func() int {
nest := left + right
left = right
right = nest
return nest
}
}
func main() {
f := fibonacci(0, 1)
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}

golang函數閉包實現波費納契數列