Go error handling, return if ?

Michael Francis
3 min readAug 12, 2022
Photo by Brett Jordan on Unsplash

In my tuples writing on Go I walked through a number of issues that Go has with error handling and specifically highlighted the lack of tuples in the language and how this impacts the handling of errors.

GoLang, Where are my tuples?. A set of grumbly gripes about GoLang… | by Michael Francis | Medium

Unfortunately, or perhaps fortunately I don’t expect tuples to be added to the language any time soon and so this pattern is not going to change. That leaves us with the challenges of handling errors. It’s well worth reading the github issues for a comprehensive discussion on errors and the proposals for Go 2.0.

Issues · golang/go (github.com)

Many of these proposals have attempted to introduce new reserved words or proposed significant changes to the way the language reads and works. I’m not going to walk through all of the pros and cons, they are well described in the issues and if you are interested, I encourage you to read them.

Below is my simple proposal, which looks like it could be added incrementally to the current stream of go development.

Why not take a lesson from if and for and define a version of return that takes a second optional argument, a Boolean condition?

The for statement has a number of variations

for i := 0; i < 10; i++ {
// Stuff
}
i := 10
for i < 10 {
// Stuff
i--
}
for {
// stuff
}

Have a look at the tour of go for more details, see A Tour of Go. The if statement has two forms, see A Tour of Go

The Return statement is already special in that there exists a zero-argument form with named returns. A Tour of Go

Let’s look at an example of where this could work. We often write code that passes the error up the stack, in this example I’m using the two-argument form of if:

if x, err := foo(); err != nil { 
return nil, err
}
// note x and err not available here

Now if we define return to have a new form, one with two parts.

return <values to return> ; <boolean condition>

We could write the multi-line if statement as

return nil, err ; err != nil

you now have a short form that short circuits and returns from the function. It doesn’t change the semantics of the return expression to the left of the condition and defers etc. work as before.

You can mentally read this form of return as return if .

There is added potential to write expressions such as

return x, err := foo() ; err != nil 
// note x and err available here

though this last form may be a step too far and is consistent with for and if, but I can imagine use cases where this would significantly tidy up a code base while making it more unreadable.

zero argument returns continue to work, assuming the use of named arguments.

return ; err != nil

Net introduces no new symbols or keywords and is consistent with for and if

If you like this proposal, I encourage you to visit the ‘pass’ issue and give it a thumbs up.

proposal: Go 2: simplify error handling — error passing with “pass” · Issue #37141 · golang/go (github.com)

--

--