Sobes.tech
Junior — Middle

What happens if you try to close a channel that is already closed?

sobes.tech AI

Answer from AI

In Go, attempting to close an already closed channel will cause a panic during program execution. This is because the close() function is intended for a single closure of a channel, and closing it again is considered an error.

Example:

ch := make(chan int)
close(ch)      // correct closure
close(ch)      // will cause panic: close of closed channel

To avoid a panic, you need to ensure that the channel is closed only once, for example, by using additional logic or synchronization.