go
runtime_error
ai_generated
true
WARNING: DATA RACE - Write at 0x00c000012345 by goroutine 6
ID: go/data-race-on-variable
80%Fix Rate
87%Confidence
0Evidence
2024-06-25First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.22 | active | — | — | — |
Root Cause
Concurrent reads and writes to a shared variable without synchronization.
generic中文
并发读写共享变量而没有同步。
Workarounds
-
95% success
var mu sync.Mutex var x int // Write mu.Lock() x = 1 mu.Unlock() // Read mu.Lock() _ = x mu.Unlock()
-
90% success
var x atomic.Int32 x.Store(1) _ = x.Load()
Dead Ends
Common approaches that don't work:
-
50% fail
Reads still race unless also atomic.
-
100% fail
Go doesn't have volatile; no effect.
-
90% fail
Sleep doesn't establish happens-before relationship.