go
runtime_error
ai_generated
true
WARNING: DATA RACE Goroutine 5 在 0x00c0000b4010 处写入:
WARNING: DATA RACE Write at 0x00c0000b4010 by goroutine 5:
ID: go/race-detector-data-race
80%修复率
90%置信度
0证据数
2024-02-28首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.23 | active | — | — | — |
根因分析
两个 goroutine 在无同步的情况下访问同一内存位置,且至少一个是写操作。仅在以 -race 构建时被检测到。
English
Two goroutines access the same memory location without synchronization, and at least one is a write. Detected only when the binary is built with -race.
解决方案
-
95% 成功率
var counter atomic.Int64 counter.Add(1) n := counter.Load()
-
92% 成功率
ch := make(chan int) go func() { ch <- compute() }() result := <-ch
无效尝试
常见但无效的做法:
-
95% 失败
Hides the symptom; the race still corrupts data nondeterministically.
-
90% 失败
Yielding does not create a happens-before relationship; race remains.