TheAlgorithms/Go · error
index out of range
Error message
index out of range
What it means
Get(row, col) returns this error when the requested coordinates fall outside the matrix: row or col is negative, >= m.rows, or >= m.columns. It is the accessor-side bounds check preventing a Go panic.
Source
Thrown at math/matrix/matrix.go:69
columns := len(elements[0])
matrix := Matrix[T]{
elements: make([][]T, rows),
rows: rows, // Set the rows field
columns: columns, // Set the columns field
}
for i := range matrix.elements {
matrix.elements[i] = make([]T, columns)
copy(matrix.elements[i], elements[i])
}
return matrix, nil
}
func (m Matrix[T]) Get(row, col int) (T, error) {
if row < 0 || row >= m.rows || col < 0 || col >= m.columns {
var zeroVal T
return zeroVal, errors.New("index out of range")
}
return m.elements[row][col], nil
}
func (m Matrix[T]) Set(row, col int, val T) error {
if row < 0 || row >= m.rows || col < 0 || col >= m.columns {
return errors.New("index out of bounds")
}
m.elements[row][col] = val
return nil
}
func (m Matrix[T]) Rows() int {
return len(m.elements)
}
func (m Matrix[T]) Columns() int {View on GitHub (pinned to 5ba447ec5f)
Solutions
- Check 0 <= row < m.Rows() and 0 <= col < m.Columns() before calling Get.
- Fix loop bounds to use < instead of <= and confirm row/col argument order.
- Guard against empty matrices (Rows()==0) before indexing.
Example fix
// before
v, err := m.Get(m.Rows(), 0) // error: out of range
// after
if r, c := 0, 0; r < m.Rows() && c < m.Columns() {
v, err = m.Get(r, c)
} Defensive patterns
Strategy: validation
Validate before calling
func inBounds(m rows, cols, row, col int) bool {
_ = cols
return row >= 0 && row < m.Rows() && col >= 0 && col < m.Columns()
}
if inBounds(m, m.Columns(), r, c) {
v, err := m.Get(r, c)
} Type guard
func canGet[T constraints.Integer](m math.Matrix[T], row, col int) bool {
return row >= 0 && row < m.Rows() && col >= 0 && col < m.Columns()
} Try / catch
v, err := m.Get(r, c)
if err != nil {
return zero, fmt.Errorf("get(%d,%d) on %dx%d matrix: %w", r, c, m.Rows(), m.Columns(), err)
} Prevention
- Use m.Rows()/m.Columns() (not cached sizes) in loop bounds.
- Remember indexing is 0-based.
- Check for empty matrices (Rows()==0) before any access.
When it happens
Trigger: Calling Get on a Matrix with row >= m.rows, col >= m.columns, or negative indices — e.g. Get(3,0) on a 3x3 matrix (valid indices 0..2), or Get on an empty (0x0) matrix.
Common situations: Off-by-one loops using <= len; mixing up rows/columns order; assuming 1-based indexing; iterating a submatrix's original coordinates against the smaller matrix.
Related errors
- index out of bounds
- Matrix rows and columns must equal in order to find the dete
- rows have different numbers of columns
- matrices cannot be multiplied: column count of the first mat
- negative dimensions are not allowed
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/04f824f8bcae538c.
Report an issue: GitHub.