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

proposal: Go 2: When dealing with err, add a wildcard #50207

Closed
misaka123456 opened this issue Dec 16, 2021 · 1 comment
Closed

proposal: Go 2: When dealing with err, add a wildcard #50207

misaka123456 opened this issue Dec 16, 2021 · 1 comment
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge LanguageChange Proposal
Milestone

Comments

@misaka123456
Copy link

misaka123456 commented Dec 16, 2021

  err := func()
  if err != nil {
     return errpkg.Wrap(err)
  }

can change it to
~ = fun()

相当于如果~的类型为error,并且A()的返回值为error,并且~不为nil,这自动转换为 return errpkg.Wrap(err)
Equivalent to if the type of ~ is error, and the return value of A() is error, and ~ is not nil, this is automatically converted to return errpkg Wrap(err)

for example

fun A() error {
  ~ = fun()
}
// 相当于,Equivalent to
fun A() error {
  err := func()
  if err != nil {
     return errpkg.Wrap(err)
  }
}
fun A() (user error) {
  ~ = fun()
}
// 相当于,Equivalent to
fun A() error {
  err := func()
  if err != nil {
     return nil, errpkg.Wrap(err)
  }
}
@gopherbot gopherbot added this to the Proposal milestone Dec 16, 2021
@misaka123456 misaka123456 changed the title proposal: Go 2: proposal: Go 2: When dealing with err, add a wildcard Dec 16, 2021
@randall77
Copy link
Contributor

Dup of #42214 using a different token.

@ianlancetaylor ianlancetaylor added error-handling Language & library change proposals that are about error handling. LanguageChange labels Dec 17, 2021
@golang golang locked and limited conversation to collaborators Dec 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge LanguageChange Proposal
Projects
None yet
Development

No branches or pull requests

4 participants