# fatal error: 并发读写 map

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

## 根因

一个 goroutine 读 map 的同时另一个 goroutine 写该 map。Go 运行时 map 无内置锁，检测到读写竞态即终止。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0+ | active | — | — |

## 解决方案

1. **** (97% 成功率)
   ```
   var mu sync.RWMutex
func get(k string) (V, bool) {
    mu.RLock(); defer mu.RUnlock()
    v, ok := m[k]; return v, ok
}
func set(k string, v V) {
    mu.Lock(); defer mu.Unlock()
    m[k] = v
}
   ```
2. **** (92% 成功率)
   ```
   type shard struct { mu sync.RWMutex; m map[string]V }
shards := make([]shard, 32)
idx := fnv32(k) % 32
shards[idx].mu.Lock()
shards[idx].m[k] = v
shards[idx].mu.Unlock()
   ```

## 无效尝试

- **** — Struct fields are not atomic; concurrent access still races and may not even be detected, causing silent corruption. (80% 失败率)
- **** — The race detector adds large overhead and must be built in; it does not prevent the fatal error in non-race builds. (70% 失败率)
