liveBook · Manning

Multiple return values

As explained in Go in Practice, Second Edition

Multiple return values are function results in which a single invocation provides more than one distinct value.

A Go function can return distinct values from one call. Declare their types in the function signature, return a matching value for each, and assign each result to a variable. Use the blank identifier (`_`) when a result is deliberately unused. section 1.2.1

Listing 1.1 Multiple returns
package main

import (
    "fmt"
)

func getStrings() (string, string) {      #1
    return "Foo", "Bar"                  #2
}

func main() {
    n1, n2 := getStrings()     #3
    fmt.Println(n1, n2)        #3
    n3, _ := getStrings()     #4
    fmt.Println(n3)           #4
}

The function returns two strings; its callers either capture both or discard the second with `_`. section 1.2.1

Return values can also be named in the signature. Those names act as variables inside the function, and a bare `return` sends back their current values. More commonly, multiple returns pair a result with an `error`; by convention, the error comes last. section 1.2.1 section 4.1

Listing 4.1 Returning an error
package main

import (
     "errors"         #1
     "strings"
)

func Concat(parts ...string) (string, error) {       #2
     if len(parts) == 0 { 
            return "", errors.New("No strings supplied")      #3
     }                                                        #3
     return strings.Join(parts, " "), nil      #4
}

`Concat` returns a joined string and `nil` on success, or an empty string and an error when given no strings. section 4.1

Check the error before trusting the result. An empty string may be a valid result as well as the value returned on failure, so discarding the error can hide the difference. When an error accompanies a value, choose a useful value where appropriate and document what callers receive under error conditions. section 4.1 section 4.1.1

result, err := Concat(args...)
if err != nil {
    // handle err
}
// do something with result

Capture both results, handle a non-nil error, then use the result. section 4.1

How Go in Practice, Second Edition covers it

How deep it goes
Shows return signatures, assignments, named returns, and error checks, without exploring runtime internals.
Examples
Uses a two-string function and a string concatenator with successful and missing-input paths.
What it assumes
Starts with basic Go functions and variables, introducing multiple-result signatures and the blank identifier directly.
What you can do after
Build it from scratch: define Go functions with multiple results and check errors before relying on associated values.
How it connects
Connects multiple returns to error handling, nil, zero values, named returns, and the blank identifier.

Also explained in

Compare how 2 books explain multiple return values →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.