Skip to content

Instantly share code, notes, and snippets.

@itherunder
Created December 30, 2021 09:36
Show Gist options
  • Save itherunder/b611ba5a67354494f28c85cc4f84a688 to your computer and use it in GitHub Desktop.
Save itherunder/b611ba5a67354494f28c85cc4f84a688 to your computer and use it in GitHub Desktop.
just learn,golang 使用闭包实现一个fibonacci 函数
package main
import "fmt"
// 返回一个“返回int的函数”
func fibonacci() func() int {
a, b := 1, 0
return func() int {
a, b = a+b, a
return b
}
}
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