{"record":{"id":"04f824f8bcae538c","repo":"TheAlgorithms/Go","slug":"index-out-of-range","errorCode":null,"errorMessage":"index out of range","messagePattern":"index out of range","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"math/matrix/matrix.go","lineNumber":69,"sourceCode":"\n\tcolumns := len(elements[0])\n\tmatrix := Matrix[T]{\n\t\telements: make([][]T, rows),\n\t\trows:     rows,    // Set the rows field\n\t\tcolumns:  columns, // Set the columns field\n\t}\n\tfor i := range matrix.elements {\n\t\tmatrix.elements[i] = make([]T, columns)\n\t\tcopy(matrix.elements[i], elements[i])\n\t}\n\n\treturn matrix, nil\n}\n\nfunc (m Matrix[T]) Get(row, col int) (T, error) {\n\tif row < 0 || row >= m.rows || col < 0 || col >= m.columns {\n\t\tvar zeroVal T\n\t\treturn zeroVal, errors.New(\"index out of range\")\n\t}\n\treturn m.elements[row][col], nil\n}\n\nfunc (m Matrix[T]) Set(row, col int, val T) error {\n\tif row < 0 || row >= m.rows || col < 0 || col >= m.columns {\n\t\treturn errors.New(\"index out of bounds\")\n\t}\n\n\tm.elements[row][col] = val\n\treturn nil\n}\n\nfunc (m Matrix[T]) Rows() int {\n\treturn len(m.elements)\n}\n\nfunc (m Matrix[T]) Columns() int {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/matrix/matrix.go#L51-L87","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nv, err := m.Get(m.Rows(), 0) // error: out of range\n\n// after\nif r, c := 0, 0; r < m.Rows() && c < m.Columns() {\n    v, err = m.Get(r, c)\n}","handlingStrategy":"validation","validationCode":"func inBounds(m rows, cols, row, col int) bool {\n    _ = cols\n    return row >= 0 && row < m.Rows() && col >= 0 && col < m.Columns()\n}\n\nif inBounds(m, m.Columns(), r, c) {\n    v, err := m.Get(r, c)\n}","typeGuard":"func canGet[T constraints.Integer](m math.Matrix[T], row, col int) bool {\n    return row >= 0 && row < m.Rows() && col >= 0 && col < m.Columns()\n}","tryCatchPattern":"v, err := m.Get(r, c)\nif err != nil {\n    return zero, fmt.Errorf(\"get(%d,%d) on %dx%d matrix: %w\", r, c, m.Rows(), m.Columns(), err)\n}","preventionTips":["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."],"tags":["matrix","out-of-range","bounds-check","go"],"backgroundTag":"index-out-of-range","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}