go
runtime_error
ai_generated
true
WARNING: DATA RACE Write at 0x00c000... by goroutine 7 Previous write at 0x00c000... by goroutine 8
ID: go/race-detector-write-write
80%Fix Rate
90%Confidence
0Evidence
2024-04-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.22 | active | — | — | — |
Root Cause
Two goroutines access the same memory location without synchronization, with at least one write, detected by the race detector when running with -race.
generic中文
两个 goroutine 在没有同步的情况下访问同一内存位置,且至少有一次写操作,在 -race 模式下被竞态检测器捕获。
Workarounds
-
95% success
var mu sync.Mutex mu.Lock() counter++ mu.Unlock() // or atomic.AddInt64(&counter, 1)
-
92% success
results := make(chan int, len(items)) for _, it := range items { it := it go func(v int) { results <- compute(v) }(it) }
Dead Ends
Common approaches that don't work:
-
95% fail
The race is real; disabling detection does not fix corruption, which surfaces later as random crashes.
-
90% fail
Sleep reduces the window but does not eliminate the race; the detector still reports it.