# 运行时：协程栈超过1000000000字节限制

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

## 根因

协程存在无限递归或过多的栈分配，导致栈溢出

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.18 | active | — | — |
| 1.21 | active | — | — |

## 解决方案

1. **Refactor recursive function to iterative approach** (90% 成功率)
   ```
   func factorial(n int) int {
    result := 1
    for i := 2; i <= n; i++ {
        result *= i
    }
    return result
}
   ```
2. **Use tail recursion with proper termination condition** (85% 成功率)
   ```
   func factorialTail(n, acc int) int {
    if n == 0 {
        return acc
    }
    return factorialTail(n-1, n*acc)
}
   ```

## 无效尝试

- **Increasing stack size via runtime.GOMAXPROCS** — GOMAXPROCS controls parallelism, not stack size; stack limit is fixed by runtime (95% 失败率)
- **Using more goroutines to distribute work** — More goroutines do not reduce stack usage per goroutine; each still has its own stack (80% 失败率)
