Multiple return values
A Go function can return several values in one call. The usual pair is (result, error). The caller receives them as a list of assignments, not as a tuple object.
Detailed Explanation
os.Open returns a file and an error. strconv.Atoi returns an int and an error. map lookup returns a value and a bool. The language has no Result<T, E> type in the standard library; it has multiple results. That choice is why Go error handling is a sequence of if err != nil instead of ? or exceptions.
The signature lists results in order. func split(s string) (string, string) returns two strings. Callers write head, tail := split(s). There is no result[0]. If you need to pass the pair around as one value, define a struct.
(T, error) is a convention, not a constraint. The last result should be error when the function can fail. On failure, return the zero T and a non-nil error. On success, return a useful T and nil. Callers should not use T when err != nil unless the documentation says both are meaningful (rare; io.Reader.Read is the famous exception: n > 0 with err != nil is allowed).
You cannot skip a middle result without _. v, _ := f() discards the second. v := f() does not compile if f returns two values, except when f is used as an argument to another function that takes those types in order (fmt.Println(f()) is legal).
Comma-ok is the same mechanism. v, ok := m[k], v, ok := x.(T), v, ok := <-ch are not special return syntax in your functions; they are built-in operations that produce two results. You can write the same shape yourself: func lookup(id int) (User, bool).
Named results are optional. func split(s string) (head, tail string) documents the results and lets you return without listing values. Bare returns are easy to misread; see named returns.
What it costs. Returning two words is cheap. Returning a large struct plus an error copies the struct on every success path. Return a pointer when the value is large or already on the heap. Do not invent a generic Result wrapper unless you are writing a library that must compose errors in a way (T, error) cannot; most Go code should stay with the pair.
Code Examples
package main
import (
"fmt"
"strconv"
)
func main() {
n, err := strconv.Atoi("8080")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(n)
}func div(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("divide by zero")
}
return a / b, nil
}func first(ids []int) (int, bool) {
if len(ids) == 0 {
return 0, false
}
return ids[0], true
}Common Gotchas
Using the value when `err != nil` is almost always wrong
the value is the zero, unless the function documents otherwise (`Read`).
`f()` as a single value does not compile when `f` returns two
assign both, or pass the call to something that accepts both.
There is no destructuring beyond assignment
you cannot write `split(s).0`. Name the results or wrap them in a struct if the pair is a real concept.
Wrapping one of several results still returns all of them
`return 0, fmt.Errorf("load: %w", err)` replaces the error and zeros the rest.