Introduction
Unlike many languages that only allow a single return value, Go functions can return multiple values at once. This feature is widely used for returning a result alongside an error, avoiding the need for exceptions or output parameters that other languages rely on.
Cricket analogy: Like an umpire's decision returning both "out" and the reason (LBW, caught) in one signal, Go functions return a result and an error together instead of just a lone verdict.
Syntax
func divide(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("division by zero")
}
return a / b, nil
}Explanation
When a function returns more than one value, the return types are wrapped in parentheses. The caller must handle all returned values, typically using multiple assignment such as result, err := divide(10, 2). Go also supports named return values, where the return variables are declared in the signature itself, e.g. func split(sum int) (x, y int). With named returns, a naked return statement (just 'return' with no arguments) sends back the current values of those named variables.
Cricket analogy: Like a scoreboard function declared to return (runs, wickets int) up front, a naked return at the end of the over sends back whatever runs and wickets were already tallied, no need to restate them.
Example
package main
import (
"errors"
"fmt"
)
func divide(a, b int) (int, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return // naked return
}
func main() {
result, err := divide(10, 2)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result)
}
a, b := split(17)
fmt.Println(a, b)
}
// Output:
// Result: 5
// 7 10Key Takeaways
- Multiple return types are declared in parentheses, e.g. (int, error).
- The (value, error) pattern is Go's idiomatic replacement for exceptions.
- Named return values let you declare result variables in the function signature.
- A naked return sends back the current values of named return variables.
- Callers should check the error before trusting the other returned values.
Practice what you learned
1. How does a Go function declare that it returns two values?
2. What is the idiomatic Go pattern for signaling a failure from a function?
3. What does a 'naked return' refer to?
4. In func split(sum int) (x, y int), where are x and y initialized?
Was this page helpful?
You May Also Like
Functions in Go
Learn how to declare, call, and use functions as first-class values in Go.
Error Handling in Go
Learn Go's idiomatic error handling using the built-in error interface and explicit error return values instead of exceptions.
Custom Errors and errors.Is/As in Go
Build custom error types in Go and use errors.Is and errors.As to inspect and match wrapped errors.
Variadic Functions in Go
Learn how to write and call functions that accept a variable number of arguments.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics