闭包+defer实现
func fiboClosure () (func() int) {
n := 0
cur := 1
return func() int {
defer func(){n, cur = n + cur, n}()
fmt.Print(n+cur, "\n")
return n + cur
}
}
func main () {
f := fiboClosure()
for i:=0; i < 10; i++ {
f()
}
}
推荐文章: