我不明白为什么这不会编译。它说函数结束时没有返回语句,但是当我在else后面添加return时,它仍然不会编译。
func (d Foo) primaryOptions() []string{
if(d.Line == 1){
return []string{"me", "my"}
}
else{
return []string{"mee", "myy"}
}
}
Go强制else
与if
在同一行。因为它的"自动分号插入"规则
所以一定是这个
if(d.Line == 1) {
return []string{"me", "my"}
} else { // <---------------------- this must be up here
return []string{"mee", "myy"}
}
否则,编译器会为您插入一个分号:
if(d.Line == 1) {
return []string{"me", "my"}
}; // <---------------------------the compiler does this automatically if you put it below
else {
return []string{"mee", "myy"}
}
. .所以你错了。我将很快链接到相关文件。
编辑:Effective Go有关于这方面的信息