{
  "id": "go/panic-runtime-error-close-of-nil-channel",
  "signature": "panic: runtime error: close of nil channel",
  "signature_zh": "恐慌：运行时错误：关闭空通道",
  "regex": "panic: runtime error: close of nil channel",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Attempting to close a channel variable that has not been initialized (nil) using close(ch).",
  "root_cause_type": "generic",
  "root_cause_zh": "尝试使用 close(ch) 关闭一个尚未初始化（空值）的通道变量。",
  "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": "Wrap close(ch) in a recover() block to catch the panic",
      "why_fails": "Recovering doesn't fix the root cause and may mask a logic error",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Use select with default to check if channel is nil before closing",
      "why_fails": "select doesn't detect nil channels at compile time; it's a runtime check that still panics if nil",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Set ch = make(chan T) before every close call",
      "why_fails": "Overwrites the existing channel and may cause goroutine leaks or lost messages",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Initialize the channel with make() before using: ch := make(chan T); defer close(ch)",
      "success_rate": 0.95,
      "how": "Initialize the channel with make() before using: ch := make(chan T); defer close(ch)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Guard close with a nil check: if ch != nil { close(ch) } else { /* handle nil case */ }",
      "success_rate": 0.93,
      "how": "Guard close with a nil check: if ch != nil { close(ch) } else { /* handle nil case */ }",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use sync.Once to ensure close is called only once and after initialization: var closeOnce sync.Once; closeOnce.Do(func() { close(ch) })",
      "success_rate": 0.9,
      "how": "Use sync.Once to ensure close is called only once and after initialization: var closeOnce sync.Once; closeOnce.Do(func() { close(ch) })",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "使用 make() 初始化通道后再使用：ch := make(chan T); defer close(ch)",
    "在 close 前进行空值检查：if ch != nil { close(ch) } else { /* 处理空值情况 */ }",
    "使用 sync.Once 确保 close 只调用一次且在初始化之后：var closeOnce sync.Once; closeOnce.Do(func() { close(ch) })"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://go.dev/ref/spec#Close",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.89,
  "fix_success_rate": 0.95,
  "resolvable": "true",
  "first_seen": "2023-01-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}