go
runtime_error
ai_generated
true
WARNING: DATA RACE Write at 0x... by goroutine N: main.foo() Previous read at 0x... by goroutine M: main.bar()
ID: go/race-detector-warning
80%Fix Rate
90%Confidence
0Evidence
2024-12-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.1+ | active | — | — | — |
Root Cause
Two goroutines accessed the same memory location without synchronization, at least one being a write. Detected only when built with -race.
generic中文
两个 goroutine 在无同步的情况下访问同一内存位置,且至少有一个是写操作。仅在 -race 构建时能被检测到。
Workarounds
-
97% success
var mu sync.Mutex mu.Lock() counter++ mu.Unlock() // or atomic.AddInt64(&counter, 1)
-
92% success
for _, item := range items { item := item // capture by value go func() { process(item) }() }
Dead Ends
Common approaches that don't work:
-
90% fail
Does not create a happens-before relationship between the two accesses; race detector still fires.
-
95% fail
Hides real bugs; production may corrupt data or crash non-deterministically.