# 警告：数据竞争

- **ID:** `go/race-detector-warning-data-race`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

两个 goroutine 并发访问同一内存位置，至少一个是写操作，且没有同步。由 -race 构建检测。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   var mu sync.Mutex
mu.Lock()
shared++
mu.Unlock()
   ```
2. **** (92% 成功率)
   ```
   var n int64
atomic.AddInt64(&n, 1)
   ```

## 无效尝试

- **** — Hides the bug; corrupted state and crashes still occur in production. (90% 失败率)
- **** — Go has no volatile; atomic fences alone don't fix logical races. (80% 失败率)
