{"record":{"id":"5df241e77e93284a","repo":"TheAlgorithms/Go","slug":"index-out-of-range-5df241","errorCode":null,"errorMessage":"index out of range","messagePattern":"index out of range","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"structure/dynamicarray/dynamicarray.go","lineNumber":90,"sourceCode":"\t}\n\n\treturn da.ElementData[index], nil\n}\n\n// IsEmpty function is check that the array has value or not\nfunc (da *DynamicArray) IsEmpty() bool {\n\treturn da.Size == 0\n}\n\n// GetData function return all value of array\nfunc (da *DynamicArray) GetData() []any {\n\treturn da.ElementData[:da.Size]\n}\n\n// CheckRangeFromIndex function it will check the range from the index\nfunc (da *DynamicArray) CheckRangeFromIndex(index int) error {\n\tif index >= da.Size || index < 0 {\n\t\treturn errors.New(\"index out of range\")\n\t}\n\treturn nil\n}\n\n// NewCapacity function increase the Capacity\nfunc (da *DynamicArray) NewCapacity() {\n\tif da.Capacity == 0 {\n\t\tda.Capacity = defaultCapacity\n\t} else {\n\t\tda.Capacity = da.Capacity << 1\n\t}\n\n\tnewDataElement := make([]any, da.Capacity)\n\n\tcopy(newDataElement, da.ElementData)\n\n\tda.ElementData = newDataElement\n}","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/dynamicarray/dynamicarray.go#L72-L108","documentation":"DynamicArray.CheckRangeFromIndex validates that an index lies within [0, Size). Put, Remove, and Get call it first and return this error instead of letting Go panic with a slice index-out-of-range at a deeper level.","triggerScenarios":"Calling Get(i), Put(i, v), or Remove(i) where i < 0 or i >= da.Size — e.g. indexing by the array's Capacity instead of Size, using a stale saved index after a Remove, or an off-by-one loop bound (i <= len).","commonSituations":"Iterating with <= instead of <; caching indexes across mutations; confusing capacity with size; porting 1-based logic into a 0-based API.","solutions":["Validate 0 <= index < da.Size (or len(da.Slice())) before calling","Fix loop bounds to i < size, not i <= size","Use the array's current Size() after every mutation instead of a cached length"],"exampleFix":"// before\nv, _ := arr.Get(arr.Capacity()) // index beyond size\n// after\nidx := arr.Capacity() - 1\nif idx >= 0 && idx < arr.Size() {\n    v, err := arr.Get(idx)\n    if err != nil { return err }\n}","handlingStrategy":"validation","validationCode":"func inRange(da *dynamicarray.DynamicArray, i int) bool {\n    return i >= 0 && i < da.Size()\n}\nif !inRange(arr, idx) { return ErrBadIndex }","typeGuard":null,"tryCatchPattern":"if err := arr.CheckRangeFromIndex(idx); err != nil {\n    return fmt.Errorf(\"get %d: %w\", idx, err)\n}\nv, _ := arr.Get(idx)","preventionTips":["Index by Size(), never Capacity()","Recompute bounds after every Add/Remove","Use i < size (not <=) in loops"],"tags":["go","data-structures","dynamic-array","bounds-check","index-out-of-range"],"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"}