我无法弄清楚为什么这不会编译.它说函数在没有return语句的情况下结束,但是当我在else之后添加一个return时,它仍然无法编译.
func (d Foo) primaryOptions() []string{
if(d.Line == 1){
return []string{"me", "my"}
}
else{
return []string{"mee", "myy"}
}
}
Run Code Online (Sandbox Code Playgroud)
由于它的"自动分号插入"规则,强制else与if括号位于同一行.
所以一定是这样的:
if(d.Line == 1) {
return []string{"me", "my"}
} else { // <---------------------- this must be up here
return []string{"mee", "myy"}
}
Run Code Online (Sandbox Code Playgroud)
否则,编译器会为您插入分号:
if(d.Line == 1) {
return []string{"me", "my"}
}; // <---------------------------the compiler does this automatically if you put it below
else {
return []string{"mee", "myy"}
}
Run Code Online (Sandbox Code Playgroud)
..因为你的错误.我将很快链接到相关文档.