# 警告：数据竞争
Goroutine 7 在 0x00c0000b4010 处读取：

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

## 根因

竞争检测器（go test -race / go run -race）发现对同一内存位置的无同步并发访问。通常是共享结构体字段或切片索引在无 mutex/atomic 保护下访问。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   var hits atomic.Int64
hits.Add(1)
n := hits.Load()
   ```
2. **** (97% 成功率)
   ```
   type Counter struct {
    mu sync.Mutex
    n  int
}
func (c *Counter) Inc() { c.mu.Lock(); c.n++; c.mu.Unlock() }
   ```
3. **** (92% 成功率)
   ```
   results := make(chan Result)
go func() { results <- compute() }()
r := <-results // ownership transferred
   ```

## 无效尝试

- **** — Hides the bug; production crashes or corrupts state under real concurrency. (95% 失败率)
- **** — Does not establish happens-before; race remains and detector still fires. (85% 失败率)
