Sobes.tech
Junior — Middle

Is it allowed to send data to a channel that is not open for reading and writing?

sobes.tech AI

Answer from AI

In Go, you cannot 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 — 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

Thus, data can only be sent to open channels.