# Go: cannot assign to struct field users["bob"].Age in map

**Code:** `cannot assign to struct field m[k].f in map` · **Category:** Go · **Seen on:** Go

Map values are not addressable, so you cannot modify a field of a struct stored directly in a map.

## 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

1. `u := m[k]; u.Age = 5; m[k] = u`
2. Store pointers in the map
3. Use a slice index when possible (`s[i].Age = 5` works)

## Detailed fix by platform

### Go

1. `users := map[string]*User{}; users["bob"] = &User{}; users["bob"].Age = 5`

## How to diagnose

1. **Map** — Value or pointer type?
2. **Fix** — Copy-modify-store or pointers?
3. **Concurrency** — Needs a mutex?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/go/cannot-assign-to-struct-field-in-map)
