# panic: close of closed channel

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

## Root Cause

Calling close() on a channel that has already been closed, often due to multiple goroutines closing without coordination.

## 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 once sync.Once
once.Do(func() { close(ch) })
   ```
2. **Use a closed flag with mutex** (90% success)
   ```
   var mu sync.Mutex
var closed bool
mu.Lock()
if !closed {
    close(ch)
    closed = true
}
mu.Unlock()
   ```

## Dead Ends

- **Using recover() to catch panic and ignore** — Recover hides the error but doesn't prevent data race or inconsistent state. (80% fail)
- **Checking ch != nil before close** — A closed channel is not nil; nil check doesn't prevent double close. (100% fail)
