Checking if channel has data available
suggest changeReceiving on a channel blocks if there is no data in the channel.
What if you don’t want to block?
You might be tempted to check whether the channel has data before receiving.
That check cannot be made reliable. Between the check and the receive, another goroutine can take the value. len on a channel has the same race.
To avoid waiting forever, use a timeout or a non-blocking receive with select.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents