Last active
May 14, 2019 02:21
-
-
Save lepig/48e2a90c7ad72507bae154e6d6e0b8d3 to your computer and use it in GitHub Desktop.
斐波那契数列-Golang闭包Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//output:
1
1
2
3
5
8
13
21
34
55