# 警告：数据竞争

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

## 根因

多个协程并发访问同一变量且未同步，至少有一个是写操作

## 版本兼容性

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

## 解决方案

1. **Use mutex to protect shared variable access** (90% 成功率)
   ```
   var mu sync.Mutex
var counter int
mu.Lock()
counter++
mu.Unlock()
   ```
2. **Use channels to communicate instead of sharing memory** (85% 成功率)
   ```
   ch := make(chan int)
go func() {
    ch <- 1
}()
val := <-ch
   ```

## 无效尝试

- **Adding time.Sleep to reduce race likelihood** — Sleep does not synchronize access; race condition still exists and may manifest under different loads (95% 失败率)
- **Using atomic operations on non-atomic types** — Atomic package only works on specific types; using it incorrectly can cause data races (80% 失败率)
