# 恐慌：运行时错误：索引超出范围 [5]，长度为 5（在 goroutine 中）

- **ID:** `go/goroutine-panic-uncaught`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

goroutine 访问数组或切片的越界索引，导致恐慌未被恢复，整个程序崩溃。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0 | active | — | — |
| 1.23 | active | — | — |

## 解决方案

1. **Add a deferred recover inside the goroutine to handle panics gracefully** (90% 成功率)
   ```
   go func() {
    defer func() {
        if r := recover(); r != nil {
            log.Printf("Recovered in goroutine: %v", r)
        }
    }()
    arr := make([]int, 5)
    arr[5] = 10 // panic here
}()
   ```
2. **Check slice bounds before accessing** (95% 成功率)
   ```
   arr := make([]int, 5)
index := 5
if index < len(arr) {
    arr[index] = 10
} else {
    log.Println("Index out of bounds")
}
   ```

## 无效尝试

- **Adding recover in main but not in the goroutine** — Panic in goroutine is not caught by main's recover unless deferred in the goroutine itself. (80% 失败率)
- **Using a larger array but not fixing the index logic** — Band-aid solution; index may still go out of bounds in other cases. (70% 失败率)
