# panic: runtime error: slice bounds out of range [:6] with capacity 4

- **ID:** `go/panic-runtime-error-slice-bounds-out-of-range-with-capacity`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Attempting to reslice a slice beyond its capacity, which is a compile-time check bypassed by dynamic index.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| go1.21 | active | — | — |
| go1.22 | active | — | — |
| go1.23 | active | — | — |

## Workarounds

1. **Use make to pre-allocate slice with sufficient capacity: s := make([]int, 0, 10); then append or reslice within cap.** (90% success)
   ```
   Use make to pre-allocate slice with sufficient capacity: s := make([]int, 0, 10); then append or reslice within cap.
   ```
2. **Check capacity before slicing: if cap(s) >= 6 { s = s[:6] } else { /* handle error */ }** (85% success)
   ```
   Check capacity before slicing: if cap(s) >= 6 { s = s[:6] } else { /* handle error */ }
   ```

## Dead Ends

- **Adding bounds check with if condition but using wrong variable** — Often developers check len(s) but then use cap(s) for slicing, missing the capacity limit. (65% fail)
- **Using append to extend slice without understanding capacity growth** — append may reallocate, but the original slice's capacity remains; slicing after append on old reference still fails. (50% fail)
