Go · Stage 6 – Reusable Code · Lesson 39 of 64

Named Return Values

Name result variables in a function signature.

Go code

func dimensions() (width int, height int) {
    width = 640
    height = 480
    return
}

fmt.Println(dimensions())
Line-by-line explanation
  1. Width and height are named results.
  2. The body assigns them.
  3. Bare return sends the named values.
  4. The call receives both.
Expected output
640 480

This is the expected result.

Named returns can document short functions, but bare returns become confusing in long functions. Explicit returns are often clearer.