{"record":{"id":"9a4b272e73c61668","repo":"krahets/hello-algo","slug":"error-9a4b27","errorCode":null,"errorMessage":"索引越界","messagePattern":"索引越界","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"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/codes/go/chapter_array_and_linkedlist/my_list.go#L21-L57","documentation":"A Go panic triggered by the get() method of a custom dynamic-array list when index falls outside [0, arrSize). Go has no exceptions, so the library uses panic() to signal an unrecoverable bounds violation. The panic must be caught with a deferred recover() or — preferably — prevented by bounds-checking before the call. The check uses arrSize (logical length), not arrCapacity (allocated length), so accessing any slot between arrSize and arrCapacity-1 also panics.","triggerScenarios":"Calling get(i) with i >= l.size(); passing a negative index (common in ports from Python); using a loop variable that exceeds arrSize after elements were removed.","commonSituations":"Looping to capacity() instead of size(); reusing a stale index after remove(); porting Python code that relies on negative indexing which Go does not support.","solutions":["Validate index >= 0 && index < l.size() before calling get()","Use l.size() as the loop upper bound, not capacity()","If panic recovery is needed, wrap the call in a deferred recover() goroutine-local handler"],"exampleFix":"// before\nval := l.get(i)\n\n// after\nif i >= 0 && i < l.size() {\n    val := l.get(i)\n} else {\n    val = -1\n}","handlingStrategy":"validation","validationCode":"if index >= 0 && index < l.size() {\n    val := l.get(index)\n} else {\n    val = -1\n}","typeGuard":null,"tryCatchPattern":"func safeGet(l *myList, index int) (val int, err error) {\n    defer func() {\n        if r := recover(); r != nil {\n            val = -1\n            err = fmt.Errorf(\"index %d out of bounds\", index)\n        }\n    }()\n    return l.get(index), nil\n}","preventionTips":["Always validate index >= 0 && index < l.size() before get()","Go does not support negative indexing; reject or remap negative values","Use recover() only as a last resort; pre-validation is idiomatic Go"],"tags":["data-structure","dynamic-array","go","index-bounds","panic"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}