go
runtime_error
ai_generated
true
WARNING: DATA RACE Write at 0x00c0000... by goroutine N:
ID: go/data-race-detected
80%Fix Rate
90%Confidence
0Evidence
2024-02-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.1+ | active | — | — | — |
Root Cause
Two goroutines access the same memory location concurrently, at least one is a write, and there is no synchronization; detected when running with -race.
generic中文
两个 goroutine 并发访问同一内存位置,其中至少一个是写操作,且没有同步;在使用 -race 运行时被检测到。
Workarounds
-
93% success
Protect the shared variable with sync/atomic: atomic.AddInt64(&counter, 1) v := atomic.LoadInt64(&counter)
-
95% success
Guard the critical section with a sync.Mutex and keep the section minimal.
Dead Ends
Common approaches that don't work:
-
90% fail
Hides the symptom; the race still corrupts data in production and can cause random crashes.
-
80% fail
Atomic on only one side does not establish a happens-before relationship; the race persists.