panic: runtime error: index out of range - 11 Causes and Fixes in Go
panic: runtime error: index out of range means you asked for an element at a position that does not exist, and Go killed the goroutine rather than hand you garbage. Modern Go appends the specifics, so you usually see something like panic: runtime error: index out of range [3] with length 3 or panic: runtime error: index out of range 0 with length 0 - and those two numbers are the whole diagnosis. The index is what your code asked for. The length is what the slice, array, or string actually had. If the index equals the length, you have an off-by-one in a loop bound. If the length is 0 , the collection was empty or nil and the real bug is upstream, in whatever was supposed to fill it. If the index is negative, you fed a not-found sentinel like -1 straight into a subscript. Below are 11 distinct causes, most common first, each with a reproduction you can paste into main.go and the fix that differs from it by the lines that matter. After that, a diagnostic checklist for the case tha...