Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提案: 错误处理,避免不必要的 err != nil #65875

Closed
lvan100 opened this issue Feb 22, 2024 · 1 comment
Closed

提案: 错误处理,避免不必要的 err != nil #65875

lvan100 opened this issue Feb 22, 2024 · 1 comment
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

Comments

@lvan100
Copy link

lvan100 commented Feb 22, 2024

从 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 可以不仅仅返回默认值,例子我就不写了。

@seankhliao seankhliao added LanguageChange Suggested changes to the Go language v2 An incompatible library change Proposal error-handling Language & library change proposals that are about error handling. labels Feb 22, 2024
@seankhliao
Copy link
Member

see previously rejected proposals such as #63380 #14066 #36390 #60779 #62253

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

2 participants