i'm still trying to learn golang when i chance upon the code below. i'm aware of function return types like int or float, and such but not a function. how does one interpret the return value of below code?
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci(x int) func() int {
if x <= 1 {
return x
} else {
return fibonacci(x-1) + fibonacci(x-2)
}
return 0
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
when run, it shows below errors:
./prog.go:9:10: cannot use x (variable of type int) as type func() int in return statement
./prog.go:11:10: invalid operation: operator + not defined on fibonacci(x - 1) (value of type func() int)
./prog.go:13:9: cannot use 0 (untyped int constant) as func() int value in return statement
./prog.go:17:7: not enough arguments in call to fibonacci
have ()
want (int)