Sobes.tech
Junior — Middle

Mis juhtub, kui proovite sulgeda juba suletud kanalit?

sobes.tech AI

Vastus AI-lt

In Go, attempting to close an already closed channel during program execution will cause a panic. This is because the close() function is intended to close a channel only once, and repeated closing is considered an error.

Example:

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

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