刚接触golang,写fib()感觉还是比较惊艳的。
// fib.go
package main
import "fmt"
func fib() func() int{
first,second := 0,1
return func() int{
ret := first
first,second = second,first+second
return ret
}
}
func main(){
f := fib() //函数赋给变量
for i := 0; i<1000; i++{
fmt.Println(f())
}
}
4GB内存下结果:
time ./fib 0.00s user 0.00s system 64% cpu 0.010 total