go-learning/exercises/fibonacci.go

25 lines
378 B
Go
Raw Permalink Normal View History

2023-07-17 13:11:00 +03:00
package main
// exercise link: https://go.dev/tour/moretypes/26
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
first := 0
second := 1
return func() int {
second += first
first = second - first
return first
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}