# error: all goroutines see final loop variable value (i = N) instead of their iteration value

- **ID:** `go/loop-variable-capture-goroutine`
- **Domain:** go
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Before Go 1.22, the loop variable is shared across iterations. Closures launched as goroutines capture the same variable, which by the time they run has advanced to the final value.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.16 | active | — | — |
| 1.20 | active | — | — |
| 1.22 | active | — | — |

## Workarounds

1. **** (98% success)
   ```
   for i := 0; i < n; i++ {
    i := i
    go func() { use(i) }()
}
   ```
2. **** (97% success)
   ```
   // go.mod: go 1.22
for i := 0; i < n; i++ {
    go func() { use(i) }()
}
   ```

## Dead Ends

- **** — Sleeps are not deterministic; the variable is still shared and may advance before the goroutine reads it. (85% fail)
- **** — Serializing execution does not change variable capture semantics; the final value bug persists. (80% fail)
