# panic: runtime error: index out of range [0] with length 0 (in goroutine)

- **ID:** `go/goroutine-slice-concurrent-access`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Multiple goroutines append to a slice without synchronization, causing the slice to be corrupted or accessed incorrectly.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.21 | active | — | — |
| 1.22 | active | — | — |

## Workarounds

1. **Use sync.Mutex to protect slice append** (98% success)
   ```
   var mu sync.Mutex
var s []int
for i := 0; i < 10; i++ {
    go func(val int) {
        mu.Lock()
        s = append(s, val)
        mu.Unlock()
    }(i)
}
   ```
2. **Use a channel to collect results** (95% success)
   ```
   ch := make(chan int, 10)
for i := 0; i < 10; i++ {
    go func(val int) {
        ch <- val
    }(i)
}
var s []int
for i := 0; i < 10; i++ {
    s = append(s, <-ch)
}
   ```

## Dead Ends

- **Pre-allocating slice capacity** — Pre-allocation does not prevent concurrent append races; the length field is still modified concurrently. (80% fail)
- **Using a local slice per goroutine and merging later** — Merging slices concurrently can still cause races if not synchronized. (70% fail)
