Skip to content

Instantly share code, notes, and snippets.

@lepig
Last active May 14, 2019 02:21
Show Gist options
  • Save lepig/48e2a90c7ad72507bae154e6d6e0b8d3 to your computer and use it in GitHub Desktop.
Save lepig/48e2a90c7ad72507bae154e6d6e0b8d3 to your computer and use it in GitHub Desktop.
斐波那契数列-Golang闭包Demo
package main
import "fmt"
func main() {
f := fibonacci()
for i:=0; i<10; i++ {
fmt.Println(f())
}
}
// 斐波那契数列(闭包)
func fibonacci() func() int {
n := 1
j := 0
return func() int {
k := n + j
n,j = j,k
return k
}
}
@lepig
Copy link
Author

lepig commented May 14, 2019

//output:

1
1
2
3
5
8
13
21
34
55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment