Programming Languages
Go
Subjective
Oct 04, 2025
What is the select statement and when is it used?
Detailed Explanation
The select statement in Go:
• Chooses between multiple channel operations
• Blocks until one operation can proceed
• Random selection if multiple ready
• Default case for non-blocking
• Used for timeouts and cancellation
Example:
select {
case msg := <-ch1:
fmt.Println("Received:", msg)
case ch2 <- "hello":
fmt.Println("Sent hello")
case <-time.After(1 * time.Second):
fmt.Println("Timeout")
default:
fmt.Println("No communication")
}
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts