{"record":{"id":"c16ebce130e86ad4","repo":"TheAlgorithms/Go","slug":"index-out-of-bounds","errorCode":null,"errorMessage":"index out of bounds","messagePattern":"index out of bounds","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"math/matrix/matrix.go","lineNumber":76,"sourceCode":"\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 {\n\tif len(m.elements) == 0 {\n\t\treturn 0\n\t}\n\treturn len(m.elements[0])\n}\n","sourceCodeStart":58,"sourceCodeEnd":93,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/matrix/matrix.go#L58-L93","documentation":"Set(row, col, val) returns this error when the target coordinates are outside the matrix (negative or >= row/column count). The matrix is left unmodified. Note the message differs slightly from Get's (\"out of bounds\" vs \"out of range\").","triggerScenarios":"Calling Set with row >= m.rows, col >= m.columns, or negative values — e.g. Set(1,3,v) on a 3-column matrix, or any Set on an empty 0x0 matrix.","commonSituations":"Off-by-one fill loops (i <= size), writing to coordinates computed from another (larger) matrix, 1-based index habits, resizing logic that shrank the matrix but not the loop.","solutions":["Validate 0 <= row < m.Rows() and 0 <= col < m.Columns() before Set.","Correct loop bounds (use i < rows, j < cols) and argument order.","Re-create the matrix with New(rows, cols, zero) if it is smaller than needed."],"exampleFix":"// before\nfor i := 0; i <= 3; i++ { m.Set(i, 0, v) } // errors at i == 3\n\n// after\nfor i := 0; i < m.Rows(); i++ { m.Set(i, 0, v) }","handlingStrategy":"validation","validationCode":"if r < 0 || r >= m.Rows() || c < 0 || c >= m.Columns() {\n    return fmt.Errorf(\"cannot set (%d,%d) on %dx%d matrix\", r, c, m.Rows(), m.Columns())\n}\nerr := m.Set(r, c, v)","typeGuard":"func canSet[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":"if err := m.Set(r, c, v); err != nil {\n    return fmt.Errorf(\"set(%d,%d) failed: %w\", r, c, err)\n}","preventionTips":["Bound fill loops with i < m.Rows(), j < m.Columns().","Reuse the same guard helper for Get and Set.","Re-create the matrix with New() when a larger canvas is needed instead of writing out of bounds."],"tags":["matrix","out-of-bounds","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"}