go
runtime_error
ai_generated
true
警告:数据竞态 由 goroutine 7 在 0x00c0000b4010 处写入: main.main.func1()
WARNING: DATA RACE Write at 0x00c0000b4010 by goroutine 7: main.main.func1()
ID: go/race-condition-detected-race
80%修复率
90%置信度
0证据数
2024-09-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
两个 goroutine 并发访问同一内存位置,且至少有一个是写操作,未使用同步。使用 -race 标志运行时被检测到。
English
Two goroutines accessed the same memory location concurrently, at least one being a write, without synchronization. Detected when running with the -race flag.
解决方案
-
95% 成功率
var mu sync.Mutex mu.Lock() counter++ mu.Unlock()
-
90% 成功率
var counter int64 atomic.AddInt64(&counter, 1) n := atomic.LoadInt64(&counter)
无效尝试
常见但无效的做法:
-
95% 失败
The race still exists in production and can cause corrupted data or crashes; the flag only hides detection.
-
80% 失败
Mixing atomic and non-atomic access to the same variable is itself a race and still flagged by the detector.