Select with multiple channels
📖 Scenario: You are building a simple program that listens to two different message channels. Each channel sends a message at different times. You want to print the message as soon as it arrives from either channel.
🎯 Goal: Create two channels that send messages, then use a select statement to wait for messages from both channels and print them as they arrive.
📋 What You'll Learn
Create two channels called
chan1 and chan2 of type chan stringCreate a variable called
done of type chan bool to signal when to stopUse a
select statement inside a for loop to receive messages from chan1 and chan2Print the message received from either channel with
fmt.PrintlnStop the loop after receiving 2 messages total and send
true to done💡 Why This Matters
🌍 Real World
Listening to multiple data sources or events at the same time is common in real-world programs like servers, chat apps, or sensors.
💼 Career
Understanding how to use Go channels and select statements is important for writing concurrent programs and working with real-time data streams.
Progress0 / 4 steps