02 / 08

How does Go handle multiple return values, and what are named returns?

Go functions can return multiple values natively, which is the idiomatic way to return both a result and an error. Named returns pre-declare result variables in the function signature.

Multiple return values are a first-class feature in Go, most commonly used for the (result, error) pattern. This makes error handling explicit and visible at every call site.

Multiple return values
Named returns guidelines
  1. 1

    Named returns improve readability in short functions by documenting what each return value represents

  2. 2

    Allow bare return statements — return without arguments uses the named variables

  3. 3

    Can be used with defer to modify return values after the function body executes

  4. 4

    Avoid in long functions — bare returns hide what is actually being returned and hurt readability

  5. 5

    Named returns are especially useful for defer-based cleanup that modifies error return