go syntax ai_generated true

multiple-value f() (value of type (T, error)) used in single-value context

ID: go/multiple-value-in-single-context

Also available as: JSON · Markdown
99%Fix Rate
99%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
121 active
1 active

Root Cause

Calling a function that returns multiple values (typically value, error) in a context that expects a single value.

generic

Workarounds

  1. 99% success Assign both return values: val, err := fn()
    Handle the error: val, err := fn(); if err != nil { return err }

    Sources: https://go.dev/doc/effective_go#multiple-returns

  2. 85% success Use _ to explicitly discard: val, _ := fn()
    Only when you're certain the error cannot occur or is irrelevant

Dead Ends

Common approaches that don't work:

  1. Wrapping the function to discard the error 85% fail

    Silently ignores errors, leading to nil pointer panics

Error Chain

Frequently confused with: