# fatal error: concurrent map read and map write

- **ID:** `go/goroutine-map-concurrent-read-write`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

One goroutine reads from a map while another goroutine writes to it without synchronization.

## Version Compatibility

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

## Workarounds

1. **Use sync.RWMutex for read-write protection** (97% success)
   ```
   var mu sync.RWMutex
m := make(map[string]int)
// write
mu.Lock()
m["key"] = 1
mu.Unlock()
// read
mu.RLock()
val := m["key"]
mu.RUnlock()
   ```
2. **Use a single goroutine to own the map and communicate via channels** (95% success)
   ```
   ch := make(chan request)
go func() {
    m := make(map[string]int)
    for req := range ch {
        switch req.op {
        case "read":
            req.resp <- m[req.key]
        case "write":
            m[req.key] = req.val
        }
    }
}()
   ```

## Dead Ends

- **Using a read-only copy of the map** — Copying a map during concurrent writes is also racy. (75% fail)
- **Using a channel to serialize access** — If not done correctly, it can introduce deadlocks. (60% fail)
