go
runtime_error
ai_generated
true
fatal error: concurrent slice append
ID: go/goroutine-racy-slice-append
80%Fix Rate
85%Confidence
0Evidence
2024-10-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
Multiple goroutines appending to the same slice without synchronization, causing data race.
generic中文
多个协程向同一个slice追加元素而没有同步,导致数据竞争。
Workarounds
-
90% success Use mutex to protect slice append
var mu sync.Mutex var s []int mu.Lock() s = append(s, 1) mu.Unlock()
-
85% success Use channel to collect results
ch := make(chan int, 10) for i := 0; i < 10; i++ { go func(val int) { ch <- val }(i) } var s []int for i := 0; i < 10; i++ { s = append(s, <-ch) }
Dead Ends
Common approaches that don't work:
-
Pre-allocating slice with make to avoid reallocation
80% fail
Append still modifies slice header; concurrent writes race on length.
-
Using copy-on-write pattern manually
70% fail
Complex and error-prone; still racy without synchronization.