assignment mismatch: 1 variable but f returns 2 values Go Origin: Application & API Last updated 4 Nov 2026

Go: assignment mismatch: 1 variable but os.Open returns 2 values

The number of variables on the left does not match the number of values the function returns.

Exact message you may see assignment mismatch: 1 variable but returns 2 valuesassignment mismatch: 2 variables but f returns 1 valueassignment mismatch: 1 variable but os.Open returns 2 valuesf() (no value) used as valuemultiple-value in single-value context
Seen on: Go

Meaning

Go functions often return (value, error). Capture both (f, err := os.Open(p)) or discard with _. The same error appears when a function returns nothing but you assign its result.

Common causes

  • Ignoring the error return
  • Function changed to return an extra value
  • Using the result of a function that returns nothing
  • Wrong number in return statement
  • Range over map with one variable of wrong kind

⚡ Quick fix

  1. f, err := os.Open(p); if err != nil { … }
  2. Use _ for values you don't need
  3. Update callers after signature changes

Detailed fix by platform

Go

  1. Never ignore errors silently; run go vet and errcheck to find them

How to diagnose

  1. Returns — How many values?
  2. Left — Variables count?
  3. Error — Handled?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

cannot assign to struct field m[k].f in map — Go: cannot assign to struct field users["bob"].Age in map (Go)cannot assign to stru…cannot refer to unexported name — Go: cannot refer to unexported name pkg.helper (undefined: pkg.helper) (Go)cannot refer to unexp…cannot use x as type in assignment/argument — Go: cannot use "5" (untyped string constant) as int value in argument to f (Go)cannot use x as type …cgo: C compiler gcc not found — Go: cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in $PATH (Go)cgo: C compiler gcc n…Client.Timeout exceeded while awaiting headers — Go: net/http: request canceled (Client.Timeout exceeded while awaiting headers) (Go)Client.Timeout exceed…context canceled — Go: context canceled (rpc error: code = Canceled desc = context canceled) (Go)context canceledcontext deadline exceeded — Go: Get "https://api.example.com": context deadline exceeded (Client.Timeout exceeded while awaiting headers) (Go)context deadline exce…dial tcp: connect: connection refused — Go: dial tcp 127.0.0.1:5432: connect: connection refused (Go)dial tcp: connect: co…assignmen…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.