Summary: in this tutorial, you will learn how to define Go functions that return multiple values.
Defining functions that return multiple values
Go functions can return multiple values. To define a function with multiple return values, you specify the types of return values in parentheses:
func functionName() (type1, type2, ...) {
// function body
}
Code language: JavaScript (javascript)
For example, the following defines a swap
function that swaps the values of two variables and return two corresponding values:
package main
import "fmt"
func swap(x int, y int) (int, int) {
return y, x
}
func main() {
x, y := 10, 20
x, y = swap(x, y)
fmt.Println(x, y)
}
Code language: Go (go)
Output:
20 10
Code language: Go (go)
Typically, functions return the second value as an error. For example:
package main
import (
"errors"
"fmt"
)
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("cannot divide by zero")
}
return a / b, nil
}
func main() {
result, err := divide(4, 2)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result)
}
result, err = divide(4, 0)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result)
}
}
Code language: Go (go)
How it works.
Step 1. Define a function divide()
that divides two floats and returns a result or error:
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("cannot divide by zero")
}
return a / b, nil
}
Code language: Go (go)
If b is zero, the function returns two values 0
and error
. To return an error, we call the new()
function (or method) of the built-in errors
package.
If b is not zero, the function returns the division of a and b as the first return value and nil as the second value. In Go, nil
is zero values for pointers. We’ll cover it in the pointer tutorial.
Step 2. Call the divide()
function to divide 5.2
by 2
:
result, err := divide(5.2, 2)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result)
}
Code language: Go (go)
If the err
is not nil
, display an error. Otherwise, display the result. In this case, it’ll display the following on the screen:
Result: 2.6
Code language: Go (go)
Step 3. Call the divide()
function to divide 5 by 0:
result, err = divide(5, 0)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result)
}
Code language: Go (go)
In this case, the divide returns nil so the output is:
Error: cannot divide by zero
Code language: Go (go)
Why Go allows functions to return multiple values
Go focuses on simplicity, clarity, and explicit error handling. By allowing functions to return multiple values:
- Improve error handling: Functions often return an error as the second value. This approach allows Go to handle errors explicitly.
- Enhance simplicity: Instead of creating complex structures to store multiple return values, returning multiple values from functions is more concise.
- Avoid side effects: functions that return multiple values can communicate all necessary information back to the caller without modifying parameters. This keeps the functions pure and avoids the side effects.
Summary
- Go functions can return multiple values.
- Return the second value as an error and handle it explicitly in the caller.