I cannot figure out why this will not compile. It says functions ends without a return statement, but when I add a return after the else, it still won't compile.
func (d Foo) primaryOptions() []string{
if(d.Line == 1){
return []string{"me", "my"}
}
else{
return []string{"mee", "myy"}
}
}
Go forces else
to be on the same line as the if
brace.. because of its "auto-semicolon-insertion" rules.
So it must be this:
if(d.Line == 1) {
return []string{"me", "my"}
} else { // <---------------------- this must be up here
return []string{"mee", "myy"}
}
Otherwise, the compiler inserts a semicolon for you:
if(d.Line == 1) {
return []string{"me", "my"}
}; // <---------------------------the compiler does this automatically if you put it below
else {
return []string{"mee", "myy"}
}
..hence your error. I will link to the relevant documentation shortly.
EDIT: Effective Go has information regarding this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With