Multiple Return Values
Fast recall
Quick revision
Core answer
Whether you understand Multiple Return Values beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The form:
func divide(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("division by zero")
}
return a / b, nil
}
q, err := divide(10, 2) // 5, nil
q, err = divide(10, 0) // 0, errorExecuted. The convention the whole ecosystem follows: error is the LAST return value; on failure, the other values are zero/nil (don't trust them); on success, error is nil. Check err before using the values.
Multiple Return Values
Direct Answer
Go functions can return several values at once: the (value, error) pattern is the ecosystem-wide convention. Error is LAST; on failure, other values are zero. It replaced exceptions — the error is visible in the signature and checked at the call site.
Deep Dive & Architecture
Why multiple returns instead of exceptions — the design argument: with (T, error), the failure is visible in the SIGNATURE — you can't call the function without acknowledging the error slot (the compiler enforces using or explicitly discarding it). Exceptions hide the failure path — any call might throw, and you need docs to know. Go's signature IS the documentation.
Beyond errors — the honest other uses: (key, found) lookups, (min, max) computations, (value, ok) type assertions — the comma-ok pattern everywhere. Multiple returns aren't just for errors; they're Go's only way to return a compound result, and the (T, error) convention is the most common specialization.
The naming convention: return values can be named in the signature — func f() (result int, err error) — which initializes them (zero), allows naked returns, and enables defer modification (the defer topic). Named returns are documentation too: func Read(p []byte) (n int, err error) — the names in the docs ARE the signature's.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Multiple Return Values in a real project?
- How would you compare Multiple Return Values with an alternative?
- What is a common mistake when using Multiple Return Values?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.