# fatal error: concurrent map writes

- **ID:** `go/fatal-error-concurrent-map-writes`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 94%

## Root Cause

Multiple goroutines are writing to the same map without synchronization, triggering Go's runtime race detector.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| go1.20 | active | — | — |
| go1.21 | active | — | — |
| go1.22 | active | — | — |

## Workarounds

1. **Protect the map with a sync.RWMutex: var mu sync.RWMutex; mu.Lock(); m[key] = value; mu.Unlock()** (94% success)
   ```
   Protect the map with a sync.RWMutex: var mu sync.RWMutex; mu.Lock(); m[key] = value; mu.Unlock()
   ```
2. **Use sync.Map for concurrent access: var m sync.Map; m.Store(key, value); value, ok := m.Load(key)** (92% success)
   ```
   Use sync.Map for concurrent access: var m sync.Map; m.Store(key, value); value, ok := m.Load(key)
   ```
3. **Restructure code to use a single goroutine that owns the map and communicates via channels** (90% success)
   ```
   Restructure code to use a single goroutine that owns the map and communicates via channels
   ```

## Dead Ends

- **Use a map with a mutex but lock only for writes, not reads** — Reads without locking can still cause data races and panic (70% fail)
- **Replace map with sync.Map without understanding its semantics** — sync.Map is optimized for specific patterns (append-only, write-once); misuse can be slower or incorrect (60% fail)
- **Ignore the error and rely on -race flag to catch it later** — The error is fatal and crashes the program immediately; cannot be ignored (95% fail)
