{
  "id": "go/goroutine-map-concurrent-read-write",
  "signature": "fatal error: concurrent map read and map write",
  "signature_zh": "致命错误：并发map读写操作",
  "regex": "fatal\\ error:\\ concurrent\\ map\\ read\\ and\\ map\\ write",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "One goroutine reads from a map while another goroutine writes to it without synchronization.",
  "root_cause_type": "generic",
  "root_cause_zh": "一个协程读取map时，另一个协程在无同步的情况下写入该map。",
  "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 read-only copy of the map",
      "why_fails": "Copying a map during concurrent writes is also racy.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using a channel to serialize access",
      "why_fails": "If not done correctly, it can introduce deadlocks.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use sync.RWMutex for read-write protection",
      "success_rate": 0.97,
      "how": "var mu sync.RWMutex\nm := make(map[string]int)\n// write\nmu.Lock()\nm[\"key\"] = 1\nmu.Unlock()\n// read\nmu.RLock()\nval := m[\"key\"]\nmu.RUnlock()",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a single goroutine to own the map and communicate via channels",
      "success_rate": 0.95,
      "how": "ch := make(chan request)\ngo func() {\n    m := make(map[string]int)\n    for req := range ch {\n        switch req.op {\n        case \"read\":\n            req.resp <- m[req.key]\n        case \"write\":\n            m[req.key] = req.val\n        }\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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-12-01",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}