Module 3 · Lesson 2 of 512/50 in the course~12 min
Module lessons (2/5)
Multiple returns and named returns
Lesson preview5 hands-on exercises readyCourse Go · Module 3 — Functions
Returning multiple values (value, err), the named returns pattern, and when NOT to use it.
What you'll learn in this lesson
- Declare functions that return two or more values at once without creating disposable transport structs.
- Apply the standard
(result T, err error)convention in error-prone functions. - Use named return values to document returns and understand the risks of naked
return.
Overview
The ability to return multiple values directly from a function is one of Go's most idiomatic features and is the basis of explicit error handling.
When an operation can fail, the error always occupies the last position in the return list, and in case of failure the first value takes on its zero value:
Sections covered in this lesson
- 1Positional multiple returns
- 2The universal contract
(T, error) - 3Named return values and inline documentation
- 4When to prefer a return
struct - 5Guided practice
- 6Check for understanding
Pro Lesson · Module 3 — Functions
Unlock "Multiple returns and named returns" and the full Course Go curriculum
Returning multiple values (`value, err`), the named returns pattern, and when NOT to use it.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Go plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.