Skip to content

Instantly share code, notes, and snippets.

@resouer
Created April 15, 2014 15:05
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 resouer/10739898 to your computer and use it in GitHub Desktop.
Save resouer/10739898 to your computer and use it in GitHub Desktop.
Go fibonacci by closure
package main
import "fmt"
// fibonacci 函数会返回一个返回 int 的函数。
func fibonacci() func() int {
var x, y int = -1, 1
return func() int {
x, y = y, x + y
return y
}
}
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