# fatal error: concurrent map iteration and map write

- **ID:** `go/racy-map-access`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

One goroutine is iterating over a map while another goroutine writes to it, causing a race condition.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.6 | active | — | — |
| 1.21 | active | — | — |

## Workarounds

1. **Use sync.RWMutex to protect both reads and writes** (95% success)
   ```
   var mu sync.RWMutex
mu.RLock()
for k, v := range myMap { ... }
mu.RUnlock()
mu.Lock()
myMap[key] = value
mu.Unlock()
   ```
2. **Use sync.Map for concurrent-safe map operations** (90% success)
   ```
   var m sync.Map
m.Store(key, value)
m.Load(key)
   ```

## Dead Ends

- **Using a sync.Mutex only around writes** — Iteration also needs protection; concurrent iteration and write still race. (85% fail)
- **Using a RWMutex for reads but not for iteration** — Iteration is a read operation but may still race with writes if not locked. (80% fail)
