go
runtime_error
ai_generated
true
WARNING: DATA RACE Read at 0x... by goroutine N: main.main.func1() Previous write at 0x... by goroutine M:
ID: go/data-race-on-shared-counter
80%Fix Rate
89%Confidence
0Evidence
2024-09-08First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
Root Cause
Multiple goroutines read and write a shared variable without synchronization. The race detector reports the conflicting accesses.
generic中文
多个 goroutine 在无同步的情况下读写共享变量,race detector 报告了冲突访问。
Workarounds
-
95% success
var counter int64 atomic.AddInt64(&counter, 1)
-
93% success
var mu sync.Mutex mu.Lock() counter++ mu.Unlock()
Dead Ends
Common approaches that don't work:
-
90% fail
Hides the symptom but the underlying race still corrupts data and can crash in production.
-
85% fail
unsafe does not provide synchronization; the race remains.