The panic occurs because your code attempts to send data on a channel that has already been closed. You must ensure the channel remains open before sending or close it only after all sends are complete.
// Correct: Close channel only after all sends are done
func worker(ch chan int) {
defer close(ch)
for i := 0; i < 10; i++ {
ch <- i
}
}
// Incorrect: Closing while another goroutine might still send
// func badWorker(ch chan int) {
// close(ch) // Panic if another goroutine sends here
// ch <- 1 // PANIC: send on closed channel
// }