# panic: close of closed channel

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

## Root Cause

Attempting to close a channel that has already been closed.

## Version Compatibility

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

## Workarounds

1. **Use sync.Once to ensure close is called only once** (95% success)
   ```
   var closeOnce sync.Once
closeOnce.Do(func() { close(ch) })
   ```
2. **Design with a single producer that closes the channel** (90% success)
   ```
   Only one goroutine should close the channel; use a dedicated closer goroutine.
   ```

## Dead Ends

- **Checking if channel is closed before closing via a flag** — Race condition: two goroutines may both see the flag as false and attempt close. (80% fail)
- **Using recover() to catch panic** — recover() catches panics but closing a closed channel is a programming error; better to prevent. (70% fail)
