krahets/hello-algo · error
индекс выходит за границы
Error message
индекс выходит за границы
What it means
`get(index)` in the Russian `myList` panics with "индекс выходит за границы" (index out of bounds) when the position is outside `[0, arrSize-1]`. It is the read-side bounds guard of this tutorial dynamic array.
Source
Thrown at ru/codes/go/chapter_array_and_linkedlist/my_list.go:39
extendRatio: 2, // Коэффициент увеличения списка при каждом расширении
}
}
/* Получить длину списка (текущее число элементов) */
func (l *myList) size() int {
return l.arrSize
}
/* Получить вместимость списка */
func (l *myList) capacity() int {
return l.arrCapacity
}
/* Доступ к элементу */
func (l *myList) get(index int) int {
// Если индекс выходит за границы, выбрасывается исключение; далее аналогично
if index < 0 || index >= l.arrSize {
panic("индекс выходит за границы")
}
return l.arr[index]
}
/* Обновление элемента */
func (l *myList) set(num, index int) {
if index < 0 || index >= l.arrSize {
panic("индекс выходит за границы")
}
l.arr[index] = num
}
/* Добавление элемента в конец */
func (l *myList) add(num int) {
// При превышении вместимости по числу элементов запускается расширение
if l.arrSize == l.arrCapacity {
l.extendCapacity()
}View on GitHub (pinned to 69932aed18)
Solutions
- Gate reads with `0 <= index < l.size()`.
- Loop with `i < l.size()`, not `i <= l.size()`.
- Use `size()` for valid-index arithmetic.
- Recover at boundaries that accept caller indices.
Example fix
// before
v := l.get(i) // индекс выходит за границы
// after
if i >= 0 && i < l.size() {
v = l.get(i)
} Defensive patterns
Strategy: validation
Validate before calling
if i < 0 || i >= l.size() {
return 0, errors.New("index out of bounds")
}
return l.get(i), nil Type guard
func (l *myList) validIndex(i int) bool {
return i >= 0 && i < l.arrSize
} Try / catch
defer func() {
if r := recover(); r != nil {
// return a zero value / error instead of crashing
}
}()
v = l.get(i) Prevention
- Use size(), not capacity(), for valid-index math.
- Loop with `i < l.size()`.
- Recompute indices after mutations.
When it happens
Trigger: Reading with `index < 0` or `index >= arrSize`; reading an empty list; using capacity rather than size for bounds math.
Common situations: Off-by-one loop bounds, `capacity()`/`size()` confusion, or a stale index kept across a mutating operation.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/a05722f4aed2d870.
Report an issue: GitHub.