Skip to content

Instantly share code, notes, and snippets.

@ninehills
Last active December 30, 2015 02:29
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 ninehills/7763221 to your computer and use it in GitHub Desktop.
Save ninehills/7763221 to your computer and use it in GitHub Desktop.
fibonacci 闭包 by go
package main
import "fmt"
// fibonacci 函数会返回一个返回 int 的函数。
func fibonacci() func() int {
sum1 := -1
sum2 := 1
return func() int {
sum := sum1 + sum2
sum1 = sum2
sum2 = sum
return sum
}
}
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