{
  "id": "go/goroutine-race-on-closure-variable",
  "signature": "WARNING: DATA RACE: Write by goroutine X, Read by goroutine Y (closure variable)",
  "signature_zh": "警告：数据竞争：协程X写入，协程Y读取（闭包变量）",
  "regex": "WARNING:\\ DATA\\ RACE:\\ Write\\ by\\ goroutine\\ X,\\ Read\\ by\\ goroutine\\ Y\\ \\(closure\\ variable\\)",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Goroutine closures capture loop variables by reference, causing multiple goroutines to race on the same variable.",
  "root_cause_type": "generic",
  "root_cause_zh": "协程闭包通过引用捕获循环变量，导致多个协程竞争同一个变量。",
  "versions": [
    {
      "version": "1.0",
      "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": "Adding time.Sleep in loop to stagger goroutines",
      "why_fails": "Race condition still exists; timing-dependent fix unreliable.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using global variable instead of local",
      "why_fails": "Global variable is shared; race condition worsens.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Pass loop variable as argument to goroutine",
      "success_rate": 0.99,
      "how": "for i := 0; i < 10; i++ {\n    go func(val int) {\n        fmt.Println(val)\n    }(i)\n}",
      "condition": "",
      "sources": []
    },
    {
      "action": "Create local copy within loop",
      "success_rate": 0.98,
      "how": "for i := 0; i < 10; i++ {\n    val := i\n    go func() {\n        fmt.Println(val)\n    }()\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": "2025-01-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}