# panic: close of closed channel

- **ID:** `go/close-of-closed-channel`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Two goroutines both called close(ch), or a deferred close ran twice because the function was retried. Go panics on the second close.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.0+ | active | — | — |

## Workarounds

1. **** (97% success)
   ```
   var closeOnce sync.Once
closeOnce.Do(func() { close(ch) })
   ```
2. **** (95% success)
   ```
   // Only the producer closes
func produce(ch chan<- int, n int) {
    defer close(ch)
    for i := 0; i < n; i++ { ch <- i }
}
   ```

## Dead Ends

- **** — The flag itself is racy without a mutex; two goroutines can both read false and both call close. (80% fail)
- **** — If the main goroutine is not the sole owner or the function can run concurrently, this does not guarantee single close. (60% fail)
