go
runtime_error
ai_generated
true
警告:数据竞争
WARNING: DATA RACE
ID: go/race-detector-warning-data-race
80%修复率
88%置信度
0证据数
2024-07-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
两个 goroutine 并发访问同一内存位置,至少一个是写操作,且没有同步。由 -race 构建检测。
English
Two goroutines access the same memory location concurrently, at least one is a write, without synchronization. Detected by -race build.
解决方案
-
95% 成功率
var mu sync.Mutex mu.Lock() shared++ mu.Unlock()
-
92% 成功率
var n int64 atomic.AddInt64(&n, 1)
无效尝试
常见但无效的做法:
-
90% 失败
Hides the bug; corrupted state and crashes still occur in production.
-
80% 失败
Go has no volatile; atomic fences alone don't fix logical races.