Junior — Middle
Kas on lubatud saata andmeid kanalisse, mis ei ole avatud lugemiseks ja kirjutamiseks?
sobes.tech AI
Vastus AI-lt
In Go, it is not possible to send data to a channel that is not open for writing. If the channel is closed (using close(channel)), attempting to send to it will cause a panic (runtime panic).
However, reading from a closed channel is possible — in this case, the read will return the zero value of the channel's type and a flag indicating that the channel is closed.
Example:
ch := make(chan int)
close(ch)
// Reading from a closed channel is safe
value, ok := <-ch // ok == false
// Sending to a closed channel will cause a panic
ch <- 1 // panic: send on closed channel
Therefore, data can only be sent to open channels.