go
runtime_error
ai_generated
true
警告:数据竞争 写操作 0x... 由 goroutine 7 执行: main.main.func1() /main.go:12 +0x... 之前读操作 0x... 由主 goroutine 执行: main.main() /main.go:9 +0x...
WARNING: DATA RACE Write at 0x... by goroutine 7: main.main.func1() /main.go:12 +0x... Previous read at 0x... by main goroutine: main.main() /main.go:9 +0x...
ID: go/race-detector-detected
80%修复率
89%置信度
0证据数
2024-03-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
race 检测器(-race)发现 goroutine 之间对共享内存的未同步访问。
English
The race detector (-race) found unsynchronized access to shared memory between goroutines.
解决方案
-
95% 成功率
var mu sync.Mutex mu.Lock() counter++ mu.Unlock()
-
90% 成功率
results := make(chan int, 1) go func() { results <- compute() }() v := <-results
无效尝试
常见但无效的做法:
-
95% 失败
Hides the bug; production will still corrupt data.
-
90% 失败
Reduces but does not eliminate race; detector still flags it.