Go: cannot assign to struct field users["bob"].Age in map
Map values are not addressable, so you cannot modify a field of a struct stored directly in a map.
cannot assign to struct field in mapcannot assign to struct field users["bob"].Age in mapcannot assign to m[k].f (neither addressable nor a map index expression) Meaning
Copy the struct out, modify it, and write it back, or store pointers (map[string]*User). The same applies to array/slice elements returned by value from functions.
Common causes
- Struct values in map assigned via m[k].field = x
- Method with pointer receiver called on map element
- Range loop variable copy modified
- Function return value modified
- Slice of structs copied
⚡ Quick fix
u := m[k]; u.Age = 5; m[k] = u- Store pointers in the map
- Use a slice index when possible (
s[i].Age = 5works)
Detailed fix by platform
Go
users := map[string]*User{}; users["bob"] = &User{}; users["bob"].Age = 5
How to diagnose
- Map — Value or pointer type?
- Fix — Copy-modify-store or pointers?
- Concurrency — Needs a mutex?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- assignment mismatch: 1 variable but f returns 2 values Go: assignment mismatch: 1 variable but os.Open returns 2 values
- cannot refer to unexported name Go: cannot refer to unexported name pkg.helper (undefined: pkg.helper)
- cannot use x as type in assignment/argument Go: cannot use "5" (untyped string constant) as int value in argument to f
Most viewed in Go
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 4 Nov 2026