{"record":{"id":"79c1bcb67d7f3c69","repo":"krahets/hello-algo","slug":"index-out-of-bounds-79c1bc","errorCode":null,"errorMessage":"Index out of bounds","messagePattern":"Index out of bounds","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"en/codes/go/chapter_array_and_linkedlist/my_list.go","lineNumber":39,"sourceCode":"\t\textendRatio: 2,               // Multiple by which the list capacity is extended each time\n\t}\n}\n\n/* Get list length (current number of elements) */\nfunc (l *myList) size() int {\n\treturn l.arrSize\n}\n\n/* Get list capacity */\nfunc (l *myList) capacity() int {\n\treturn l.arrCapacity\n}\n\n/* Update element */\nfunc (l *myList) get(index int) int {\n\t// If the index is out of bounds, throw an exception, as below\n\tif index < 0 || index >= l.arrSize {\n\t\tpanic(\"Index out of bounds\")\n\t}\n\treturn l.arr[index]\n}\n\n/* Add elements at the end */\nfunc (l *myList) set(num, index int) {\n\tif index < 0 || index >= l.arrSize {\n\t\tpanic(\"Index out of bounds\")\n\t}\n\tl.arr[index] = num\n}\n\n/* Direct traversal of list elements */\nfunc (l *myList) add(num int) {\n\t// When the number of elements exceeds capacity, trigger the extension mechanism\n\tif l.arrSize == l.arrCapacity {\n\t\tl.extendCapacity()\n\t}","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/go/chapter_array_and_linkedlist/my_list.go#L21-L57","documentation":"`get(index)` in the English `myList` panics with \"Index out of bounds\" when the requested position is outside `[0, arrSize-1]`. It is the read-side analogue of the bounds check on a real dynamic array. A bad index aborts the goroutine via `panic`.","triggerScenarios":"Reading with `index < 0` or `index >= arrSize`; reading from an empty list; using the backing capacity rather than the logical size.","commonSituations":"Off-by-one loop bounds, confusing `capacity()` with `size()`, or a stale index held across an `add`/`insert`/`remove` that changed the size.","solutions":["Gate every read with `0 <= index < l.size()`.","Loop with `i < l.size()`, never `i <= l.size()`.","Use `size()`, not `capacity()`, for valid-index math.","Recover at API boundaries that accept caller-supplied indices."],"exampleFix":"// before\nv := l.get(i) // panics \"Index out of bounds\"\n\n// after\nif i >= 0 && i < l.size() {\n    v = l.get(i)\n}","handlingStrategy":"validation","validationCode":"if i < 0 || i >= l.size() {\n    return 0, errors.New(\"index out of bounds\")\n}\nreturn l.get(i), nil","typeGuard":"func (l *myList) validIndex(i int) bool {\n    return i >= 0 && i < l.arrSize\n}","tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        // return a zero value / error instead of crashing on a bad read\n    }\n}()\nv = l.get(i)","preventionTips":["Use size() (not capacity()) for all valid-index math.","Loop with `i < l.size()`, never `<=`.","Recompute indices after any mutating operation."],"tags":["go","array-list","index-out-of-bounds","panic","teaching-example"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}