# fatal error: 并发 map 迭代与 map 写入

- **ID:** `go/concurrent-map-write-during-range`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

一个 goroutine 正在 range 遍历 map，同时另一个 goroutine 写入同一个 map，违反了 Go 的 map 安全规则。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.18 | active | — | — |
| 1.21 | active | — | — |
| 1.22 | active | — | — |

## 解决方案

1. **** (97% 成功率)
   ```
   var mu sync.RWMutex
mu.RLock()
for k, v := range m { _ = k; _ = v }
mu.RUnlock()
// writer:
mu.Lock()
m[k] = v
mu.Unlock()
   ```
2. **** (90% 成功率)
   ```
   var m sync.Map
m.Range(func(k, v any) bool { return true })
m.Store(key, val)
   ```

## 无效尝试

- **** — Timing-based avoidance is unreliable; the runtime can still detect concurrent access under load. (90% 失败率)
- **** — This is a fatal runtime error, not a panic; recover cannot catch it and the process exits. (95% 失败率)
