go
runtime_error
ai_generated
true
恐慌:运行时错误:索引超出范围 [3] 长度为 2(在协程中)
panic: runtime error: index out of range [3] with length 2 (in goroutine)
ID: go/goroutine-panic-unrecovered
80%修复率
84%置信度
0证据数
2024-10-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.21 | active | — | — | — |
根因分析
goroutine中访问了超出切片范围的索引,导致panic。
English
goroutine中访问了超出切片范围的索引,导致panic。
解决方案
-
90% 成功率
if i < len(slice) { v := slice[i] } -
85% 成功率
go func(){ defer func(){ if r := recover(); r != nil { log.Println(r) } }(); /* ... */ }()
无效尝试
常见但无效的做法:
-
60% 失败
检查不充分,可能仍有边界情况。
-
70% 失败
recover只能恢复,但数据可能未处理。