我想不出为什么这段代码无法编译。 它显示函数没有返回语句,但是当我在else后添加了一个return时,它仍无法编译。
func (d Foo) primaryOptions() []string{
if(d.Line == 1){
return []string{"me", "my"}
}
else{
return []string{"mee", "myy"}
}
}
我想不出为什么这段代码无法编译。 它显示函数没有返回语句,但是当我在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"}
}