Skip to main content

How to Return Multiple Values from a Function in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A Go function can hand back several results at once: a quotient and a remainder, a value and an error. You list the result types in brackets after the parameters, and the caller receives all of them in one assignment.

Syntax​

Syntax
func <name>(<parameters>) (<type>, <type>) {
return <value>, <value>
}

<a>, <b> := <name>(<arguments>)
PartWhat it does
(<type>, <type>)The result types, in order.
return <value>, <value>Returns one value per result type.
<a>, <b> :=Receives the results, in the same order.

The Go specification on return: "The return value or values may be explicitly listed in the "return" statement. Each expression must be single-valued and assignable to the corresponding element of the function's result type."

The Example​

The example divides 17 by 5 and returns both the whole-number result and the remainder.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Function​

Create a file named main.go:

main.go, which defines divide(a, b int) returning a / b and a % b, receives quotient and remainder from divide(17, 5) and prints them.

main.go
package main

import "fmt"

func divide(a, b int) (int, int) {
return a / b, a % b
}

func main() {
quotient, remainder := divide(17, 5)
fmt.Println(quotient, remainder)
}

a / b divides whole numbers and drops the fraction; a % b is what's left over.

Step 2: Run It​

Terminal
go run main.go
Output
3 2

A zsh terminal where go run main.go prints 3 2.

17 divided by 5 is 3, with 2 left over. The command is the same in zsh, bash and PowerShell.

The Most Common Use: a Value and an Error​

Many Go functions return a result and an error, such as strconv.Atoi, which returns the number and an error that's nil on success. The caller checks the error before using the value.

Common Mistakes​

MistakeWhat happensDo this instead
Receiving fewer values, as in q := divide(17, 5)assignment mismatch: 1 variable but divide returns 2 valuesReceive both, or use _ for the one you don't need
Returning fewer values than the signature listsnot enough return values with have (int) and want (int, int)Return one value per result type
Swapping the order when receivingThe variables get each other's values, with no error if the types matchKeep the same order as the signature

Troubleshooting​

SymptomLikely causeFix
assignment mismatchThe number of variables differs from the number of resultsMatch them, using _ to skip
declared and not used for one of the resultsA result you don't need got a nameReplace that name with _

FAQ​

Can results have names?​

Yes: func divide(a, b int) (quotient, remainder int) names them, and a bare return returns their current values.

Can I pass the results straight to another function?​

Yes, when that function takes exactly those parameters: fmt.Println(divide(17, 5)) prints 3 2.

Conclusion​

List the result types in brackets, return one value for each, and receive them in the same order with one assignment. Use _ for any result you don't need.

Additional Resources​