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 if channel has data before doing a receive.

You can’t do that in Go as it couldn’t possibly work correctly. Between the time you check for availability and the time you do a receive, some other goroutine could have picked up the value.

If you want to avoid infinite waiting, you can add a timeout or do a non-blocking wait by using select.

Feedback about page:

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

Table Of Contents