go
runtime_error
ai_generated
true
panic: runtime error: index out of range [5] with length 3 (in goroutine)
ID: go/goroutine-panic-in-goroutine
80%Fix Rate
87%Confidence
0Evidence
2025-03-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.22 | active | — | — | — |
Root Cause
Goroutine accesses slice or array with out-of-bounds index, causing panic that crashes the entire program if not recovered.
generic中文
协程使用越界索引访问slice或数组,导致恐慌,如果不恢复则崩溃整个程序。
Workarounds
-
90% success Use recover() inside the goroutine to handle panic
go func() { defer func() { if r := recover(); r != nil { log.Println("recovered from panic:", r) } }() arr := []int{1,2,3} fmt.Println(arr[5]) }() -
95% success Always check bounds before accessing slice
if idx < len(arr) { fmt.Println(arr[idx]) } else { log.Println("index out of bounds") }
Dead Ends
Common approaches that don't work:
-
Ignoring panic and hoping it doesn't happen
100% fail
Panic crashes the program; cannot be ignored.
-
Using recover() in main goroutine only
95% fail
Recover must be called in the same goroutine that panicked; main goroutine cannot recover from child goroutine panic.