-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Closed
Labels
Description
Lets look at the common function
func MyFunc() (*SomeStruct, int, error) {
data, err := callSomething()
if err != nil {
return nil, 0, err
}
return data, callSomethingOther(), nil
}
the
if err != nil {
return <default type value>, <default type value>, err
}
is the most common error checking code. But it can be removed by adding syntax construction like
func MyFunc() (*SomeStruct, int, error) {
data, <err> := callSomething()
return data, callSomethingOther(), nil
}
that means if the err is not nil, exit from the function and return default type values except error value. It should not compile if more than one error type returned.
It is pretty clean for reading, and removes a lot of code lines with identical error checking.