Return statements, conclusion

August 5, 2024

Just a couple small notes before we move on from return statements…

Return statements

Regardless of how they are declared, all the result values are initialized to the zero values for their type upon entry to the function. A “return” statement that specifies results sets the result parameters before any deferred functions are executed.

For the most part, this can be considered trivia, for Quiz night at your next Go meetup. The only time it makes a practical difference in day-to-day programming is when using named return values, as this is how you can modify a return value in a defer statement.

func foo() (err error) {
	f, err := f.Open("somefile.txt")
	if err != nil {
		return err
	}
	defer func() {
		if e := f.Close(); e != nil && err == nil {
			err = e
		}
	}()
	/* do something with f */
}

Implementation restriction: A compiler may disallow an empty expression list in a “return” statement if a different entity (constant, type, or variable) with the same name as a result parameter is in scope at the place of the return.

func f(n int) (res int, err error) {
	if _, err := f(n-1); err != nil {
		return  // invalid return statement: err is shadowed
	}
	return
}

In short, I consider this just one more reason to avoid naked returns.

Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Naked returns

Now we’re going to look at the third and finaly way to return values from a function in Go. But first a warning. ⚠️ NEVER DO THIS!! ⚠️ What we’re about to look at is commonly referred to as “naked returns” or “bare returns”, and it is widely (though, admittedly, not universally) considered bad practice. It’s honestly a mystery to me why this feature exists in Go, and I wish it didn’t.


Modifying return values in deferred functions

Oops! It seems that Friday’s email got stuck, and didn’t actually send until late Monday. Meaning you probably got two emails in the same day, and out of order. I hope it didn’t bother anyone too much. I should probably make a joke about how it was deferred… Nah! Today we’ll be finishing up the section on the defer keyword, with one special capability that it affords us. Defer statements …


Returning from a function without a return value

Let’s continue our discussion of return statements by looking at functions that don’t actually return anything… Return statements … In a function without a result type, a “return” statement must not specify any result values. func noResult() { return } Simple enough. Within functions without return values, return must not include any result values. But that doesn’t actually mean the example code you see is good code. In the noResult function, the return statement is actually completely redundant, because the function simply ends on the next line.

Get daily content like this in your inbox!

Subscribe