Checking if channel has data available

suggest change

Receiving 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents