Forgive my ignorance! Still working through the Go tour and I came across the Fibonacci exercise.
My initial impression is that the below code matches the Fibonacci function signature.
func fibonacci() func() int {
first, second := 0, 1
func next() int {
ret := first
first, second = second, first+second
return ret
}
return next
}
But the above function does not compile. I can't help but feel that it is more readable than an unnamed function (like below), and should be in theory equivalent. Am I just doing something wrong that I can't seem to identify yet, or is this actually just a no-no in Go?
func fibonacci() func() int {
first, second := 0, 1
return func() int {
ret := first
first, second = second, first+second
return ret
}
}