提示 if block ends with a return statement, so drop this else and outdent its block的解决方法
if block ends with a return statement, so drop this else and outdent its block.
通常这个错误是由go-lint(一个golang代码规范检测工具)提示的;他的大致意思是说:如果block以return语句结束,那么去掉这个else并去掉它的block。
这是由于你没有规范的使用 if ... else ... 导致的。它并不是语法错误或编译错误,只是你写的代码不够规范。也就是说,它并不会影响我们编译程序。
来看一段代码帮助理解:
func hahaha(a int) (rev string) {
if a == 1 {
return "okokok"
} else {
return "lue~~~"
}
}
这个函数就会触发提示,修改成以下形式即可:
func hahaha(a int) (rev string) {
if a == 1 {
return "okokok"
}
return "lue~~~"
}
我移除了else语句,并将else块中的代码移到了 if 块后面。