go
runtime_error
ai_generated
true
警告:数据竞争 Goroutine 7 写入地址 0x00c0000b4010 主 goroutine 先前读取地址 0x00c0000b4010
WARNING: DATA RACE Write at 0x00c0000b4010 by goroutine 7: main.main.func1() Previous read at 0x00c0000b4010 by main goroutine:
ID: go/race-detector-write-read
80%修复率
90%置信度
0证据数
2024-01-30首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.1+ | active | — | — | — |
根因分析
共享变量被不同 goroutine 无同步地读写。Go 内存模型不提供顺序保证,因此会出现撕裂读取和过期值。
English
A shared variable is read and written by different goroutines without synchronization. Go's memory model gives no ordering guarantees, so torn reads and stale values occur.
解决方案
-
97% 成功率
var mu sync.Mutex var counter int mu.Lock() counter++ mu.Unlock()
-
96% 成功率
var counter atomic.Int64 counter.Add(1) _ = counter.Load()
无效尝试
常见但无效的做法:
-
92% 失败
Hides the symptom; the underlying race can corrupt memory or produce wrong results silently.
-
98% 失败
Go has no volatile keyword; the compiler and CPU may still reorder accesses.