Created
December 30, 2021 09:36
-
-
Save itherunder/b611ba5a67354494f28c85cc4f84a688 to your computer and use it in GitHub Desktop.
just learn,golang 使用闭包实现一个fibonacci 函数
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" | |
// 返回一个“返回int的函数” | |
func fibonacci() func() int { | |
a, b := 1, 0 | |
return func() int { | |
a, b = a+b, a | |
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