go
runtime_error
ai_generated
true
WARNING: DATA RACE
ID: go/race-detector-found-data-race
80%Fix Rate
90%Confidence
0Evidence
2024-01-30First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.22 | active | — | — | — |
Root Cause
The race detector (go test -race / go run -race) observed two goroutines accessing the same memory location concurrently, at least one being a write, without synchronization.
generic中文
竞态检测器(go test -race / go run -race)观察到两个 goroutine 并发访问同一内存位置,至少一个是写操作,且没有同步。
Workarounds
-
92% success
Introduce sync.Mutex or sync/atomic around the shared variable; ensure happens-before via channel send/receive.
-
88% success
Pass data by value into goroutines or use immutable snapshots; run go test -race in CI to prevent regressions.
Dead Ends
Common approaches that don't work:
-
95% fail
The race still corrupts data in production; hiding it makes failures nondeterministic and worse.
-
90% fail
Sleeps reduce detection probability but do not establish a happens-before relationship.