# error: 测试失败: 所有 goroutine 捕获了相同的循环变量值

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

## 根因

在 Go 1.22 之前，循环变量在各次迭代间共享。在 for 循环中启动的闭包或 goroutine 捕获同一个变量，因此都看到最终值。

## 版本兼容性

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

## 解决方案

1. **** (97% 成功率)
   ```
   for _, v := range items {
    v := v
    go func() { use(v) }()
}
   ```
2. **** (95% 成功率)
   ```
   // go.mod: go 1.22
for _, v := range items {
    go func() { use(v) }()
}
   ```

## 无效尝试

- **** — Non-deterministic; the variable still changes under the goroutine and results vary. (85% 失败率)
- **** — Does not change the fact that the variable is shared; masking the race. (80% 失败率)
