Skip to content

Instantly share code, notes, and snippets.

@wangyangkobe
Created November 16, 2014 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangyangkobe/80bb87da159dea1dfc42 to your computer and use it in GitHub Desktop.
Save wangyangkobe/80bb87da159dea1dfc42 to your computer and use it in GitHub Desktop.
Go语言的“斐波纳契闭包”
import "fmt"
// fibonacci 函数会返回一个返回 int 的函数。
func fibonacci() func() int {
a := -1
b := 1
return func() int {
a, b = b, a+b
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