How to test if a channel is close and only send to it when it’s not closed

You can’t. The rule of thumb here is that only writers should close channels, this way you know that you shouldn’t write to that channel anymore.

Some simple code would look like this:

for i := 0; i < 100; i++ {
    value := calculateSomeValue()
    channel <- value
}

close(channel) //indicate that we will no more send values

Leave a Comment