0Pricing
Go Academy · Lesson

Multiple Return Values

Returning tuples and the error pattern

Multiple Return Values is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Multiple Returns?

Go functions can return multiple values, eliminating the need for out-parameters or exceptions. The most common pattern is returning a result and an error:

func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("division by zero")
    }
    return a / b, nil
}

Handling Multiple Returns

Callers receive all return values as a tuple. Use _ to discard values you don't need:

result, err := divide(10, 3)
if err != nil {
    log.Fatal(err)
}
fmt.Println(result)

value, _ := divide(8, 2) // discard error

Named Return Values

Return values can be named, turning them into variables within the function:

func minMax(nums []int) (min, max int) {
    min, max = nums[0], nums[0]
    for _, n := range nums[1:] {
        if n < min { min = n }
        if n > max { max = n }
    }
    return // naked return
}

Naked Returns

A return without arguments returns the named return variables. Use sparingly — only in short functions where names are self-documenting:

  • Short functions: acceptable
  • Long functions: hurts readability — prefer explicit returns

The (value, error) Pattern

The canonical Go error handling pattern:

func readConfig(path string) (Config, error) {
    data, err := os.ReadFile(path)
    if err != nil {
        return Config{}, fmt.Errorf("read config: %w", err)
    }
    var cfg Config
    return cfg, json.Unmarshal(data, &cfg)
}

Never Ignore Errors

Ignoring errors with _ is dangerous. Best practices:

  • Check every error unless you have a specific reason not to
  • Wrap errors with context using fmt.Errorf("op: %w", err)
  • Use log.Fatal or panic only at program boundaries, not deep in libraries

Three-Value Returns

Some functions return three values, though this is less common:

func parseIPPort(s string) (ip string, port int, err error) {
    // ... parsing logic
    return
}

Returning Structs vs Multiple Values

When to return a struct vs multiple values:

  • 2-3 strongly related values: multiple returns are fine
  • 4+ values or optional fields: use a struct
  • The (value, error) pattern: always multiple returns

Assigning to Existing Variables

Use = (not :=) when assigning to variables already declared outside the call:

var cfg Config
var err error
cfg, err = loadConfig()
if err != nil { ... }

Multiple Returns in Method Chaining

Go does not support method chaining on multiple return values directly (unlike Rust's Result.map). Use intermediate variables to handle errors at each step:

data, err := fetchData(url)
if err != nil { return err }
parsed, err := parseData(data)
if err != nil { return err }
return save(parsed)

Quick Check

What does a naked return do in Go?

Recap: Multiple Return Values

Multiple returns are a core Go idiom:

  • Functions can return multiple values in a parenthesized list
  • The (value, error) pattern is everywhere — always check the error
  • Named return variables enable naked returns (use sparingly)
  • Use _ to discard values you don't need
  • Prefer multiple returns over out-parameters for cleaner APIs

Next: variadic functions.

Frequently asked questions

Is the “Multiple Return Values” lesson free?

Yes — the full text of “Multiple Return Values” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Multiple Return Values”?

Returning tuples and the error pattern You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Multiple Return Values” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Function Basics and Signatures
  2. Multiple Return Values
  3. Variadic Functions
  4. defer, Anonymous Functions & Closures
← Back to Go Academy