go
runtime_error
ai_generated
true
警告: 数据竞争 地址 0x00c0000... 的写入由 goroutine N 执行:
WARNING: DATA RACE Write at 0x00c0000... by goroutine N:
ID: go/data-race-detected
80%修复率
90%置信度
0证据数
2024-02-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.1+ | active | — | — | — |
根因分析
两个 goroutine 并发访问同一内存位置,其中至少一个是写操作,且没有同步;在使用 -race 运行时被检测到。
English
Two goroutines access the same memory location concurrently, at least one is a write, and there is no synchronization; detected when running with -race.
解决方案
-
93% 成功率
Protect the shared variable with sync/atomic: atomic.AddInt64(&counter, 1) v := atomic.LoadInt64(&counter)
-
95% 成功率
Guard the critical section with a sync.Mutex and keep the section minimal.
无效尝试
常见但无效的做法:
-
90% 失败
Hides the symptom; the race still corrupts data in production and can cause random crashes.
-
80% 失败
Atomic on only one side does not establish a happens-before relationship; the race persists.