Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Keywords and Statements

The return statement

Reference for Go return: returning single and multiple values, named results and bare returns, interaction with defer, and the missing return error.

return ends the current function and optionally hands values back to the caller. If the function declares results, return must provide a value for each of them, in order. A function without results uses a plain return, or simply reaches the closing brace.

Results can be named. Named results are ordinary local variables initialized to zero values at function entry, and a "bare" return (with no operands) returns their current values. Deferred functions run after the return values are set, so they can inspect and change named results.

Go
func area(w, h int) int {
	return w * h
}

Try it

Go Single, multiple and named results

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go defer can change named results

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
return
return expr
return expr1, expr2
return f()          // f returns the right number of values
Form Description
return Allowed in functions with no results, or with named results (returns their current values).
return a, b Each expression must be assignable to the matching result type.
return f() A single call whose results match the function's results.

Errors and edge cases:

  • missing return: a function with results has a path that ends without a return, panic, or infinite loop.
  • too many return values / not enough return values: counts must match exactly.
  • A bare return is rejected when a named result is shadowed in an inner scope: result parameter x not in scope at return.
  • Returning from inside a for loop, switch or select leaves the whole function, not just the block.

Usage notes

  • Order of events: the return expressions are evaluated and stored into the result slots, then deferred functions run, then control goes back to the caller. This is why a deferred closure can modify named results.
  • Bare returns are fine in short functions where the names document the meaning. In longer functions, explicit values are easier to read.
  • Return nil for no error, and use if err != nil { return ..., err } early returns to keep the happy path unindented.
  • Do not return a nil pointer inside a non-nil interface as an error: var e *MyErr = nil; return e yields a non-nil error value. Return the literal nil.
  • Returning slices or maps shares their storage with the callee's variables; copy if the callee keeps using them.
  • Returning pointers to locals is safe in Go; the compiler moves such variables to the heap (escape analysis).
  • Methods and closures use return the same way; a return inside a function literal leaves only that literal.