# fatal error: 并发读写 map

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

## 根因

一个 goroutine 读取 map 的同时另一个 goroutine 写入该 map，且没有加锁；运行时检测到不安全的访问模式并终止进程。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.17 | active | — | — |
| 1.20 | active | — | — |
| 1.23 | active | — | — |

## 解决方案

1. **** (96% 成功率)
   ```
   var mu sync.RWMutex
mu.RLock()
v := m[k]
mu.RUnlock()
mu.Lock()
m[k] = v2
mu.Unlock()
   ```
2. **** (90% 成功率)
   ```
   var m sync.Map
m.Load(k)
m.Store(k, v)
   ```

## 无效尝试

- **** — Copying itself iterates the map while it is written, still triggering the fatal error. (85% 失败率)
- **** — Reads still race with writes; the runtime aborts on the read/write overlap. (90% 失败率)
