{
  "id": "go/panic-runtime-error-slice-bounds-out-of-range-capacity",
  "signature": "panic: runtime error: slice bounds out of range [:5] with capacity 3",
  "signature_zh": "恐慌：运行时错误：切片边界超出范围 [:5]，容量为 3",
  "regex": "panic: runtime error: slice bounds out of range \\[:(\\d+)\\] with capacity (\\d+)",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Attempting to slice a slice or array beyond its capacity, e.g., s[:5] when cap(s) is only 3.",
  "root_cause_type": "generic",
  "root_cause_zh": "尝试对切片或数组进行超出其容量的切片操作，例如当 cap(s) 仅为 3 时执行 s[:5]。",
  "versions": [
    {
      "version": "go1.20",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "go1.21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "go1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Change the slice bounds to a smaller number without checking capacity",
      "why_fails": "Arbitrary reduction may hide the real bug or cause data loss",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Ignore the panic and rely on recover() in production",
      "why_fails": "Recovering from a bounds panic is unsafe and may leave state inconsistent",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Use append() to extend the slice before slicing",
      "why_fails": "append() may reallocate, but doesn't fix the root cause of accessing beyond capacity",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Check the capacity before slicing: if cap(s) < 5 { s = append(s, make([]T, 5-cap(s))...) }; result := s[:5]",
      "success_rate": 0.95,
      "how": "Check the capacity before slicing: if cap(s) < 5 { s = append(s, make([]T, 5-cap(s))...) }; result := s[:5]",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use copy() to create a new slice with sufficient capacity: dst := make([]T, 5); copy(dst, s)",
      "success_rate": 0.93,
      "how": "Use copy() to create a new slice with sufficient capacity: dst := make([]T, 5); copy(dst, s)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Ensure the slice is initialized with sufficient capacity from the start: s := make([]T, 0, 5); s = append(s, ...)",
      "success_rate": 0.9,
      "how": "Ensure the slice is initialized with sufficient capacity from the start: s := make([]T, 0, 5); s = append(s, ...)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "在切片前检查容量：if cap(s) < 5 { s = append(s, make([]T, 5-cap(s))...) }; result := s[:5]",
    "使用 copy() 创建一个具有足够容量的新切片：dst := make([]T, 5); copy(dst, s)",
    "确保从一开始就用足够的容量初始化切片：s := make([]T, 0, 5); s = append(s, ...)"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://go.dev/ref/spec#Slice_expressions",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.92,
  "resolvable": "true",
  "first_seen": "2023-03-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}