Junior — Middle
What are the consequences of sending a message to a channel that is already closed?
sobes.tech AI
Answer from AI
In Go, attempting to send a message to a closed channel will cause a panic during program execution. This is because a closed channel does not accept new values.
Example:
ch := make(chan int)
close(ch)
ch <- 1 // panic: send on closed channel
To avoid this error, you should ensure that the channel is not closed before sending, or use constructs with select and additional flags to control the channel's state.