Multiple Return Values
func name(param type) (type1, type2) {
// body
return value1, value2
}
res1, res2 := name(arg)
Returning Two Values
A function's signature can list several return types in parentheses, and the return statement supplies a matching value for each one, comma-separated.
Example: Returning Two Values
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function divide
func divide(a, b int) (int, int) {
// Send a / b, a % b back to the caller
return a / b, a % b
}
// main is where the program starts running
func main() {
// Short declaration: create quotient, remainder and infer the type
quotient, remainder := divide(17, 5)
// Print the values followed by a newline
fmt.Println("quotient:", quotient, "remainder:", remainder)
}
Login to try C/C++/Java/PHP code in the editor
The Value, error Pattern
The most idiomatic use of multiple return values in Go is pairing a result with an error: the function returns nil for the error on success, or a non-nil error (and often a zero-value result) on failure.
Example: The Value, error Pattern
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"errors"
"fmt"
)
// Define the function safeDivide
func safeDivide(a, b int) (int, error) {
// Check whether b == 0
if b == 0 {
return 0, errors.New("division by zero")
}
// Send a / b, nil back to the caller
return a / b, nil
}
// main is where the program starts running
func main() {
// Short declaration: create result, err and infer the type
result, err := safeDivide(10, 0)
// Handle the error if one occurred
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("result:", result)
}
Login to try C/C++/Java/PHP code in the editor
Discarding Unwanted Return Values
When a function returns multiple values but you only need some of them, the blank identifier _ lets you discard the rest without declaring unused variables.
Example: Discarding Unwanted Return Values
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function minMax
func minMax(nums []int) (int, int) {
// Short declaration: create min, max and infer the type
min, max := nums[0], nums[0]
// Loop over nums; the blank identifier _ ignores the index
for _, n := range nums {
// Check whether n < min
if n < min {
min = n
}
// Check whether n > max
if n > max {
max = n
}
}
// Send min, max back to the caller
return min, max
}
func main() {
_, max := minMax([]int{4, 9, 1, 7})
fmt.Println("max:", max)
}
Login to try C/C++/Java/PHP code in the editor
- Ignoring the error return value from a function call, assuming the first value is always safe to use.
- Trying to assign only some of the returned values without using _ for the ones you're skipping.
- Forgetting that all returned values must be captured (or discarded with _) -- you can't just grab the first one positionally.
- Go functions can return multiple values, separated by commas after return.
- The most common pattern is returning a result plus an error.
- Every returned value must be assigned to a variable or explicitly discarded with _.
- Multiple return values remove the need for output parameters or wrapper objects seen in other languages.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: