递归实现
package main import ( "fmt" ) func main() { for i := 0; i < 10; i++ { nums := fibonacci(i) fmt.Println(nums) } } func fibonacci(num int) int { if num < 2 { return 1 } return fibonacci(num-1) + fibonacci(num-2) }
闭包实现
Go的闭包实现,摘自golang官网,闭包下次在详细介绍一下
package main import "fmt" // fib returns a function that returns // successive Fibonacci numbers. func fib() func() int { a,b := 0,1 return func() int { a,b = b,a+b return a } } func main() { f := fib() // Function calls are evaluated left-to-right. fmt.Println(f(),f(),f()) }原文链接:https://www.f2er.com/go/191359.html