{
  "id": "go/goroutine-slice-concurrent-access",
  "signature": "panic: runtime error: index out of range [0] with length 0 (in goroutine)",
  "signature_zh": "恐慌：运行时错误：索引超出范围 [0]，长度为0（在协程中）",
  "regex": "panic:\\ runtime\\ error:\\ index\\ out\\ of\\ range\\ \\[0\\]\\ with\\ length\\ 0\\ \\(in\\ goroutine\\)",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Multiple goroutines append to a slice without synchronization, causing the slice to be corrupted or accessed incorrectly.",
  "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": "Pre-allocating slice capacity",
      "why_fails": "Pre-allocation does not prevent concurrent append races; the length field is still modified concurrently.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using a local slice per goroutine and merging later",
      "why_fails": "Merging slices concurrently can still cause races if not synchronized.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use sync.Mutex to protect slice append",
      "success_rate": 0.98,
      "how": "var mu sync.Mutex\nvar s []int\nfor i := 0; i < 10; i++ {\n    go func(val int) {\n        mu.Lock()\n        s = append(s, val)\n        mu.Unlock()\n    }(i)\n}",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a channel to collect results",
      "success_rate": 0.95,
      "how": "ch := make(chan int, 10)\nfor i := 0; i < 10; i++ {\n    go func(val int) {\n        ch <- val\n    }(i)\n}\nvar s []int\nfor i := 0; i < 10; i++ {\n    s = append(s, <-ch)\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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-01-10",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}