{
  "id": "go/loop-variable-capture",
  "signature": "error: expected result 3, got 3 (all goroutines see same loop variable)",
  "signature_zh": "错误：期望结果 3，得到 3（所有 goroutine 看到同一个循环变量）",
  "regex": "error:\\ expected\\ result\\ 3,\\ got\\ 3\\ \\(all\\ goroutines\\ see\\ same\\ loop\\ variable\\)",
  "domain": "go",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Before Go 1.22, goroutines launched inside a for loop captured the same loop variable, so all goroutines observed the final value. This causes subtle output bugs and races.",
  "root_cause_type": "generic",
  "root_cause_zh": "在 Go 1.22 之前，for 循环内启动的 goroutine 捕获同一个循环变量，导致所有 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"
    },
    {
      "version": "1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The variable is shared; sleeping changes nothing about which value is read.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Scheduling does not affect variable capture semantics.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Shadow the loop variable: for _, v := range items { v := v; go func(){ use(v) }() }",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.92,
      "how": "Upgrade to Go 1.22+ where loop variables are per-iteration by default, or pass v as an argument: go func(v T){...}(v)",
      "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-02-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}