This post presents an interesting way to make error handling more brief in Golang 1.18
One of the downsides to this approach is that defer
functions and catching panics
can be generally more expensive than using if/return for error handling. I decided to benchmark to see how significant the penalty turns out to be.
For some boilerplate, we have a giveError function:
func giveError(b bool) (int, error) {
if b {
return 0, fmt.Errorf("given error")