go
runtime_error
ai_generated
true
panic: runtime error: index out of range
ID: go/panic-runtime-error
92%Fix Rate
92%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1 | active | — | — | — |
Root Cause
Array/slice index out of bounds. Go panics instead of returning error.
genericWorkarounds
-
95% success Add bounds check before accessing: if i < len(slice) { ... }
if i < len(slice) { ... } -
90% success Use range loop instead of index-based loop to avoid off-by-one
for i, v := range slice { ... }Sources: https://go.dev/ref/spec#For_range
Dead Ends
Common approaches that don't work:
-
Use recover() to catch all panics
80% fail
Silences the bug — index out of range means logic error
-
Pre-allocate a larger slice
70% fail
Doesn't fix the incorrect index logic
Error Chain
Frequently confused with: