Ad Space — Top Banner

runtime error: index out of range

Go Programming Language

Severity: Critical

What Does This Error Mean?

Index out of range is a runtime panic in Go — it means you tried to access a position in a slice, array, or string that does not exist. For example, accessing position 5 in a slice that only has 3 elements. Go panics and stops the program immediately. The fix is to always check that an index is within the valid range before accessing it.

Affected Models

  • Go 1.0 and later
  • All Go versions

Common Causes

  • Accessing a slice or array element at an index equal to or greater than its length
  • Using a loop counter that goes past the last element
  • An empty slice (length 0) being accessed at index 0
  • A function returns a shorter slice than expected and the code accesses an index assuming the full length
  • Off-by-one error — accessing index n when the last valid index is n-1

How to Fix It

  1. Check the length before accessing by index: if len(items) > 5 { fmt.Println(items[5]) }

    len() returns the number of elements. Valid indices are 0 to len(items)-1. Index len(items) does not exist.

  2. Use a range-based for loop instead of manual indexing: for i, v := range items { fmt.Println(i, v) } — range always stays within bounds.

    The range keyword handles the bounds automatically. It is the idiomatic Go way to iterate over slices and arrays.

  3. When accessing a specific index, always check it is valid first: if index >= 0 && index < len(items) { value = items[index] }

    Both upper and lower bounds matter — negative indices are also out of range in Go.

  4. Use recover() in a deferred function to catch panics gracefully if you cannot check bounds in advance.

    defer func() { if r := recover(); r != nil { log.Println('Recovered from panic:', r) } }() — put this at the start of the function.

  5. Add logging or print statements to check the actual length of the slice vs the index you are using: fmt.Printf('slice length: %d, trying index: %d\n', len(items), index)

    This is the fastest way to diagnose the mismatch between what you expected and what is actually in the slice.

When to Call a Professional

Index out of range panics are always fixable yourself. Always check the length of a slice before accessing elements by index. Use range-based for loops when possible — they automatically stay within bounds.

Frequently Asked Questions

What is the difference between a slice and an array in Go?

An array in Go has a fixed size declared at compile time: var arr [5]int — always 5 integers. A slice is a dynamic view into an underlying array — it has a length and capacity but can grow: var s []int Slices are far more common in Go code. Both can cause index out of range panics if accessed beyond their length.

What is a panic in Go?

A panic is Go's way of stopping execution when something goes seriously wrong that the program cannot recover from normally. When a panic occurs, Go unwinds the call stack, running any deferred functions, and then crashes. You can recover from a panic using recover() inside a deferred function. Panics are for unexpected errors — for expected errors, Go uses the error return value pattern.

How do I safely get the last element of a slice in Go?

Check that the slice is not empty first, then access the last element with index len(slice)-1: if len(items) > 0 { last := items[len(items)-1] } Do not try items[len(items)] — that is one past the end and always panics. An empty slice has length 0, and items[-1] is not valid in Go.