midGolang
Explain the `select` statement.
Updated Apr 28, 2026
Short answer
select lets a goroutine wait on multiple channel operations, executing the first one that becomes ready.
Deep explanation
It works like a switch statement but for channels. If multiple channels are ready, one is chosen at random. If none are ready, it blocks, unless a default case is provided, which makes the select non-blocking.
Real-world example
Implementing a timeout for an external API call by selecting on a channel waiting for the API response and a time.After channel.
Common mistakes
- Creating an empty `select {}` statement to block forever without realizing it halts the current goroutine permanently.
Follow-up questions
- What happens if multiple cases in a select are ready?