Go <code>select</code> without the coin flip 0 ▲ Stefan VanBuren · All Content 131 days ago · Tech · hide · Fediverse post In Go, it’s common to wait on a condition via a channel while also respecting context cancellation. A common idiom looks like: select { case <-condition: return nil case <-ctx.Done(): return ctx.Err() } An issue with this is that if you’d prefer that condition (i.e., the non-error case) be treated with precedence, you’re racing with ctx.Done(); select chooses a case at random when multiple cases are ready. My coworker suggested a solution with a nested select within the ctx.Done() case to re-check condition: select { case <-condition: return nil case <-ctx.Done(): // Re-check condition in case we raced above. select { case <-condition: return nil default: return ctx.Err() } } But I don’t care for that nested select. Looking at this Go proposal, it looks like this is the best existing idiom, but I’d love to know if there’s a better way of solving this without the nested-select smell. Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town