{
  "id": "go/goroutine-race-condition-read",
  "signature": "WARNING: DATA RACE (goroutine read)",
  "signature_zh": "警告：数据竞争（协程读操作）",
  "regex": "WARNING:\\ DATA\\ RACE\\ \\(goroutine\\ read\\)",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A goroutine reads a variable while another goroutine writes to it without synchronization.",
  "root_cause_type": "generic",
  "root_cause_zh": "一个协程读取变量时，另一个协程在无同步的情况下写入该变量。",
  "versions": [
    {
      "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": "Using a local copy of the variable",
      "why_fails": "The copy is made before the write, but the race still exists at the read point.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Adding more goroutines to distribute load",
      "why_fails": "More goroutines increase the chance of race conditions.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use sync.RWMutex for read preference",
      "success_rate": 0.97,
      "how": "var mu sync.RWMutex\nvar data map[string]int\nread := func(key string) int {\n    mu.RLock()\n    defer mu.RUnlock()\n    return data[key]\n}\nwrite := func(key string, val int) {\n    mu.Lock()\n    defer mu.Unlock()\n    data[key] = val\n}",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use channels to communicate instead of shared memory",
      "success_rate": 0.95,
      "how": "ch := make(chan int)\ngo func() {\n    v := <-ch\n    // use v\n}()\nch <- 42",
      "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.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-10-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}