{
  "id": "go/loop-variable-capture-goroutine",
  "signature": "error: all goroutines see final loop variable value (i = N) instead of their iteration value",
  "signature_zh": "error：所有 goroutine 看到的是循环变量最终值（i = N）而非各自迭代值",
  "regex": "error:\\ all\\ goroutines\\ see\\ final\\ loop\\ variable\\ value\\ \\(i\\ =\\ N\\)\\ instead\\ of\\ their\\ iteration\\ value",
  "domain": "go",
  "category": "data_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "在 Go 1.22 之前，循环变量在各迭代间共享。作为 goroutine 启动的闭包捕获同一个变量，运行时其值已推进到最终值。",
  "versions": [
    {
      "version": "1.16",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.20",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Sleeps are not deterministic; the variable is still shared and may advance before the goroutine reads it.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Serializing execution does not change variable capture semantics; the final value bug persists.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "for i := 0; i < n; i++ {\n    i := i\n    go func() { use(i) }()\n}",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.97,
      "how": "// go.mod: go 1.22\nfor i := 0; i < n; i++ {\n    go func() { use(i) }()\n}",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.9,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-10",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}