{"record":{"id":"a251f0199bcaf8a4","repo":"krahets/hello-algo","slug":"error-a251f0","errorCode":null,"errorMessage":"索引越界","messagePattern":"索引越界","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"zh-hant/codes/go/chapter_array_and_linkedlist/my_list.go","lineNumber":39,"sourceCode":"\t\textendRatio: 2,               // 每次串列擴容的倍數\n\t}\n}\n\n/* 獲取串列長度（當前元素數量） */\nfunc (l *myList) size() int {\n\treturn l.arrSize\n}\n\n/*  獲取串列容量 */\nfunc (l *myList) capacity() int {\n\treturn l.arrCapacity\n}\n\n/* 訪問元素 */\nfunc (l *myList) get(index int) int {\n\t// 索引如果越界，則丟擲異常，下同\n\tif index < 0 || index >= l.arrSize {\n\t\tpanic(\"索引越界\")\n\t}\n\treturn l.arr[index]\n}\n\n/* 更新元素 */\nfunc (l *myList) set(num, index int) {\n\tif index < 0 || index >= l.arrSize {\n\t\tpanic(\"索引越界\")\n\t}\n\tl.arr[index] = num\n}\n\n/* 在尾部新增元素 */\nfunc (l *myList) add(num int) {\n\t// 元素數量超出容量時，觸發擴容機制\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/zh-hant/codes/go/chapter_array_and_linkedlist/my_list.go#L21-L57","documentation":"Panic thrown by get(index) on the educational myList (Traditional Chinese build) when the requested index is outside [0, arrSize-1]. Reading past the live element count would touch uninitialized/stale slots, so the method enforces the bounds up front and aborts with '索引越界' (index out of bounds). get() is read-only so it cannot shrink the list, but it still requires a currently-valid index.","triggerScenarios":"Calling get() on an empty list; passing an index >= size after elements were removed; using a cached size that predates a shrink; off-by-one in an inclusive upper loop bound.","commonSituations":"Peeking the last element with get(size()-1) after a concurrent/logical removal made size() smaller; deserializing an index from untrusted input without clamping; iterating with `i <= size()` instead of `i < size()`.","solutions":["Confirm 0 <= index < l.size() before calling get().","Prefer toArray() and slice access when you need bulk reads with safe bounds.","Recompute size() immediately before indexing if the list may have changed.","Clamp or reject external indices at the input boundary."],"exampleFix":"// before: panics when index >= size\nv := l.get(idx)\n\n// after\nif idx >= 0 && idx < l.size() {\n    v := l.get(idx)\n}","handlingStrategy":"validation","validationCode":"func canGetAt(l *myList, index int) bool {\n    return index >= 0 && index < l.size()\n}\n\nif canGetAt(l, idx) {\n    v = l.get(idx)\n}","typeGuard":null,"tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        // index out of range on get\n    }\n}()\nv = l.get(idx)","preventionTips":["Always bounds-check before get(); prefer toArray() + slice for bulk reads.","Re-read size() before indexing if the list may have shrunk.","Clamp untrusted indices at the input boundary."],"tags":["go","index-out-of-bounds","list","get","validation"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}