multiple-value in single-value context
Go Programming Language
Severity: MinorWhat Does This Error Mean?
This Go error means you are using a function that returns multiple values in a place where only one value is expected. For example, Go functions often return (result, error). If you pass that function call directly into another function, Go does not know which of the two values to use. Fix it by capturing both return values into separate variables first, then use the one you need.
Affected Models
- Go 1.0 and later
- All Go versions
- go build, go run, go test
Common Causes
- Passing a multi-return function call directly as an argument to another function
- Using a multi-return function call in an expression that expects a single value
- Trying to assign only one variable from a function that returns two or more values
- Using a multi-return call inside a type conversion or slice literal
- Calling os.Open, strconv.Atoi, or other standard library functions that return (value, error) without handling both returns
How to Fix It
-
Find the function call that returns multiple values. Change it from a single expression to a two-variable assignment.
Example: change fmt.Println(strconv.Atoi(s)) to: n, err := strconv.Atoi(s) — then use n and check err separately.
-
Use two variables on the left side of := to capture both return values: result, err := myFunction()
The variable names do not matter — use descriptive names. 'result, err' is the Go convention.
-
Always check the error value before using the result. If err != nil, handle the error before proceeding.
Using the result when err is not nil is a common source of bugs. Checking errors is a core Go best practice.
-
If you genuinely do not need the error value, discard it with the blank identifier: result, _ := myFunction()
Use _ sparingly and only when you are certain the error can never occur or does not matter. Ignoring errors hides bugs.
-
Restructure nested calls that use multi-return functions by extracting them to separate lines before the outer call.
This also makes the code more readable. Deeply nested function calls with error handling are hard to read in Go.
When to Call a Professional
This is always a compile-time error. Go is strict about how multiple return values are handled. Fix it by capturing all return values into separate variables. Never ignore errors — the second return value from most Go functions is there for a reason.
Frequently Asked Questions
Why does Go return errors as values instead of using exceptions?
Go's designers believe that error handling should be explicit and visible in the code. When errors are return values, every caller must decide what to do with them. This leads to more robust code — errors cannot be silently ignored the way they can with exceptions.
Can a Go function return more than two values?
Yes, Go functions can return any number of values: func myFunc() (int, string, error) { ... } When calling such a function, capture all values: n, s, err := myFunc() Use _ to discard any values you do not need: n, _, err := myFunc()
Can I use a multi-return function call as the only return statement?
Yes, this is one special exception. You can return a multi-return function call directly if the return types match exactly. For example: func wrapper() (int, error) { return strconv.Atoi(s) } — this works because the types align. But you cannot use it inside expressions or as function arguments.