Senior
What will be the result of writing to a closed channel? Deadlock, panic, or a write operation error?
sobes.tech AI
Answer from AI
In Go, writing to a closed channel causes a panic.
If you try to send a value to a channel that is already closed, the runtime triggers a panic with the message "send on closed channel".
Example:
ch := make(chan int)
close(ch)
ch <- 1 // panic: send on closed channel
Therefore, a write operation to a closed channel does not return an error or block — it immediately causes a panic. To avoid this, you need to ensure that writing only occurs to an open channel, for example, by using additional flags or synchronization.