# panic: send on closed channel

- **ID:** `go/goroutine-send-on-closed-channel`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Sending data to a channel that has already been closed, typically due to multiple goroutines sending without proper synchronization of channel closure.

## Version Compatibility

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

## Workarounds

1. **Use a sync.Mutex to coordinate sends and close** (95% success)
   ```
   var mu sync.Mutex
ch := make(chan int)
closeCh := func() {
    mu.Lock()
    defer mu.Unlock()
    close(ch)
}
send := func(v int) {
    mu.Lock()
    defer mu.Unlock()
    ch <- v
}
   ```
2. **Use a select with default to avoid send on closed channel** (90% success)
   ```
   select {
case ch <- v:
default:
    // handle failure or retry
}
   ```

## Dead Ends

- **Checking channel state with a flag before sending** — Race condition: the channel could be closed between the check and the send, leading to a panic. (75% fail)
- **Using recover() to catch the panic and continue** — Recovering from a panic does not prevent data loss or corruption, and the program state may be inconsistent. (85% fail)
