Golang return multiple values from a function

Golang return multiple values and go function return multiple values: syntax, two-value returns, mixed types, blank identifiers, (T, error), and named results.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

Golang return multiple values from a function

Go lets a golang function return more than one result without wrapping a struct. That is what people mean by golang return multiple values, go return multiple values, golang multiple return values, and go function return multiple values: list the types in parentheses after the parameters, then return expressions in the same order. Golang return two values is the same rule with a pair such as quotient and remainder or value plus error. This page walks syntax, ignoring values with _, the usual (T, error) pattern, and optional named results.

Examples were run with a recent Go toolchain on Linux. Snippets use fixed inputs so you can run them without command-line arguments.


Syntax: golang function return multiple values

Results appear in a second parenthesized list after parameters. Types only (no parameter names here). A return statement supplies one expression per result, in order.

go
package main

import "fmt"

func split(sum int) (int, int) {
	x := sum * 4 / 9
	return sum - x, x
}

func main() {
	a, b := split(17)
	fmt.Println(a, b)
}
Output

Run prints two integers that sum to 17.


Golang return two values of the same type

go
package main

import "fmt"

func mulDiv(n int64) (int64, int64) {
	return n * 2, n / 2
}

func main() {
	a, b := mulDiv(10)
	fmt.Println(a, b)
}
Output

Run prints 20 and 5 on one line.


Return multiple values golang with different types

Each position in the result list can use a different type.

go
package main

import "fmt"

func describe(n int) (int, float64) {
	return n % 2, float64(n) * 1.5
}

func main() {
	mod, scaled := describe(10)
	fmt.Println(mod, scaled)
}
Output

Run prints 0 and 15 for the sample input.


Ignore a result with the blank identifier

If you only need one value from a multi-value return, assign the others to _. This is the blank identifier, not a hyphen.

go
package main

import "fmt"

func pair(n int) (int, int) {
	return n * 2, n * 4
}

func main() {
	half, _ := pair(11)
	fmt.Println(half)
}
Output

Run prints 22.


(T, error): multiple returns and error handling

The idiomatic pattern is to return data plus an error, with nil meaning success. Details on building and checking errors are in golang return error.

go
package main

import (
	"errors"
	"fmt"
)

func double(n int) (int, error) {
	if n < 0 {
		return 0, errors.New("negative not supported")
	}
	return n * 2, nil
}

func main() {
	if v, err := double(6); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(v)
	}
	if _, err := double(-1); err != nil {
		fmt.Println(err)
	}
}
Output

Run prints 12 then the negative-input error message.


Named return values

You may name results in the signature; they behave like variables in the function body, and a bare return sends their current values. Prefer short functions when you use naked return.

go
package main

import "fmt"

func minMax(x, y int) (min, max int) {
	if x > y {
		min, max = y, x
	} else {
		min, max = x, y
	}
	return
}

func main() {
	lo, hi := minMax(2, 9)
	fmt.Println(lo, hi)
}
Output

Run prints 2 and 9.


Summary

Golang return multiple values is built into the function signature: declare every result type, then return values in order. That covers golang multiple return, return multiple values golang, and go function return style APIs without extra structs. Golang return two values is the common special case, especially (T, error) for errors alongside data. Use _ to drop a result you do not need, and treat named returns as a readability tool for small functions, not a default. For variables and scope around assignments, see variable scope in Go; for functions generally, see golang function.


References


Frequently Asked Questions

1. Do return types have to match parameter types?

No. Parameters and results are separate lists; each result position has its own type. Only the arity and types in the return list must match what you return.

2. What is the blank identifier in a, _ := f()?

The underscore discards that result position. It is not a hyphen operator; it is the blank identifier required by Go when you must ignore a value.

3. When should I use named return values?

Use them when naked return would read clearly at the end of a short function, such as min and max. Avoid them when they hide control flow in longer bodies.

4. How does multiple return relate to errors?

The common pattern is (T, error) with nil error on success; see the article on returning errors for wrapping and checks.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …