# 致命错误：并发map写操作

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

## 根因

多个协程在没有同步的情况下写入Go map，导致运行时崩溃。

## 版本兼容性

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

## 解决方案

1. **Use sync.Mutex to protect all map access** (98% 成功率)
   ```
   var mu sync.Mutex
m := make(map[string]int)
// write
mu.Lock()
m["key"] = 1
mu.Unlock()
// read
mu.Lock()
val := m["key"]
mu.Unlock()
   ```
2. **Use sync.Map for concurrent-safe map operations** (90% 成功率)
   ```
   var m sync.Map
m.Store("key", 1)
val, ok := m.Load("key")
   ```

## 无效尝试

- **Using a mutex only for writes but not reads** — Concurrent reads during writes can also cause race conditions. (80% 失败率)
- **Using a sync.Map incorrectly** — sync.Map has specific use cases; misuse can still cause races. (65% 失败率)
