提案: 错误处理,避免不必要的 err != nil #65875
Labels
error-handling
Language & library change proposals that are about error handling.
LanguageChange
Suggested changes to the Go language
Proposal
v2
An incompatible library change
从 rust 取得灵感,引入 err!! 操作,等价于 if err != nil { return } ,这个改进是微小的,向后兼容的。
修改前:
func add(a,b string) (int,error) {
ia,err :=strconv.atoi(a)
if err != nil { return }
ib,err :=strconv.atoi(b)
if err != nil { return }
return ia+ib, nil
}
修改后:
func add(a,b string) (int,error) {
ia,err!! := strconv.atoi(a)
ib,err!! := strconv.atoi(b)
return ia+ib,nil
}
得益于 go 的返回值可命名,return 可以不仅仅返回默认值,例子我就不写了。
The text was updated successfully, but these errors were encountered: