Sobes.tech
Junior — Middle

Milline käitumine on oodatav, kui proovite sulgeda kanalit, mis on juba varem suletud?

sobes.tech AI

Vastus AI-lt

In Go, attempting to close an already closed channel results in a panic.

A channel can only be closed once, to signal that no more values will be sent.

If you call close(ch) repeatedly for the same channel, the program will abruptly terminate with an error.

Example:

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

Therefore, it is important to ensure that the channel is closed only once.