I agree with @robert.
The compiler cannot guarantee that the function
will always return a value. Even though we can see that the loop will
eventually
return i value, the compiler isn't able to make this determination statically.
Compile doesn't complain about the following code. I'm no expert, but I think this is strictly compiler behavior:
func myfunc() int {
if i > 8 {
return i
}
}
}
But the following code will not compile.
func myfunc() int {
for i := 0; i <= 9; i++ {
if i > 8 {
return i
}
}
}
To
fix that (for didactic purposes only), we can add a return statement
after the loop (which will never be reached, but satisfies the
compiler):
func myfunc() int {
if i > 8 {
return i
}
}
return 0 // This line is unreachable but satisfies the compiler
}
If anyone knows more about this behavior, I would be happy to learn more.
Cleberson