# 错误：期望结果 3，得到 3（所有 goroutine 看到同一个循环变量）

- **ID:** `go/loop-variable-capture`
- **领域:** go
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在 Go 1.22 之前，for 循环内启动的 goroutine 捕获同一个循环变量，导致所有 goroutine 看到最终值。这会造成微妙的输出错误和竞态。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Shadow the loop variable: for _, v := range items { v := v; go func(){ use(v) }() }
   ```
2. **** (92% 成功率)
   ```
   Upgrade to Go 1.22+ where loop variables are per-iteration by default, or pass v as an argument: go func(v T){...}(v)
   ```

## 无效尝试

- **** — The variable is shared; sleeping changes nothing about which value is read. (90% 失败率)
- **** — Scheduling does not affect variable capture semantics. (95% 失败率)
