Sobes.tech
Senior

What will be the result of writing to a closed channel? (deadlock / panic / operation returns an error)

sobes.tech AI

Answer from AI

Writing to a closed (non-open for writing) channel in Go causes a panic.

If the channel is closed, attempting to send a value to it results in a runtime panic with the message "send on closed channel".

Example:

ch := make(chan int)
close(ch)
ch <- 1 // panic: send on closed channel

Thus, the operation does not return an error and does not block — a immediate panic occurs. To avoid this, it is common to check whether the channel is closed before sending, or to use recover to handle the panic.

What will be the result of writing to a closed… - sobes.tech