Skip to content

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

Closed
@misaka123456

Description

@misaka123456
  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)
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeLanguageChangeSuggested changes to the Go languageProposalerror-handlingLanguage & library change proposals that are about error handling.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions