I propose try-return
statements.
"try" IdentifierList ( ":=" | "=" ) ExpressionList "return" ExpressionList
e.g. try a, err := foo() return err
- Perform variable declarations or assignments.
- If the value of the rightmost variable is zero-valued, evaluate the second ExpressionList and return.
// Conventional
a, b, err := foo()
if err != nil {
return err
}
// Proposed
try a, b, err := foo() return err
try a, b, err := foo() return errors.New("A new error")
myErrorHandler := func(err error) error {
doSomething()
return err
}
try a, b, err := foo() return myErrorHandler(err)