{
  "id": "go/range-loop-variable-capture",
  "signature": "error: test failed: all goroutines captured same loop variable value",
  "signature_zh": "error: 测试失败: 所有 goroutine 捕获了相同的循环变量值",
  "regex": "error:\\ test\\ failed:\\ all\\ goroutines\\ captured\\ same\\ loop\\ variable\\ value",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Before Go 1.22, the loop variable is shared across iterations. Closures or goroutines launched in a for loop capture the same variable, so they all see the final value.",
  "root_cause_type": "generic",
  "root_cause_zh": "在 Go 1.22 之前，循环变量在各次迭代间共享。在 for 循环中启动的闭包或 goroutine 捕获同一个变量，因此都看到最终值。",
  "versions": [
    {
      "version": "1.18",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.19",
      "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.21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Non-deterministic; the variable still changes under the goroutine and results vary.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Does not change the fact that the variable is shared; masking the race.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.97,
      "how": "for _, v := range items {\n    v := v\n    go func() { use(v) }()\n}",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "// go.mod: go 1.22\nfor _, v := range items {\n    go func() { use(v) }()\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-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}