Here’s a bug:
Can you spot it? Here’s a hint:
the dreaded default case
When you provide a deafult case to a
select statement in Golang, you’re giving the program a route to take when the other cases are blocked.
What we want to happen here is for our program to block on the receive from
While we’re blocked, we don’t need to do anything, but we want to immediately print something as soon as we receive it from the channel.
Unfortunately, by providing a default case, which does nothing, we basically turn this blocking select statement into a spin lock, pinning the CPU while attempting a receive from the channel over and over and over again. Bad news.
Here’s a rewritten version that does what we want: