Effective Go里说了:In the Go libraries, you'll find that when an if statement doesn't flow into the next statement—that is, the body ends in break, continue, goto, or return—the unnecessary else is omitted. f, err := os.Open(name, os.O_RDONLY, 0) if err!=nil { return err
}
codeUsing(f)也就是说当你if完了之后状态不会再有改变,不会有其他情况发生的时候,else应该被省略。所以去掉else吧,因为编译器它看来你需要有一个default return。改好之后是这样:func example(x int) int { if x == 0 { return 5
} return x
}