Recall & Review
beginner
What is the purpose of the
select statement in Go?The
select statement lets a Go program wait on multiple channel operations. It blocks until one of the channels is ready to send or receive, then executes the corresponding case.Click to reveal answer
intermediate
How does Go decide which
select case to run if multiple channels are ready?If multiple channels are ready, Go picks one case at random to run. This prevents starvation and ensures fairness.
Click to reveal answer
beginner
What happens if none of the channels in a
select statement are ready?The
select statement blocks and waits until one of the channels becomes ready, unless there is a default case, which runs immediately if no channels are ready.Click to reveal answer
beginner
Write a simple Go
select statement that listens on two channels ch1 and ch2 and prints which channel received data.select {
case msg1 := <-ch1:
fmt.Println("Received from ch1:", msg1)
case msg2 := <-ch2:
fmt.Println("Received from ch2:", msg2)
}
Click to reveal answer
intermediate
What is the role of the
default case in a select statement?The
default case runs immediately if no other channel operations are ready. It prevents blocking and can be used to perform non-blocking checks.Click to reveal answer
What does the
select statement do in Go?✗ Incorrect
The
select statement waits on multiple channels and runs the case for the first ready channel.If multiple channels are ready in a
select, how does Go choose which case to run?✗ Incorrect
Go picks one ready case at random to avoid starvation.
What happens if no channels are ready and there is no
default case in a select?✗ Incorrect
Without a
default, select waits (blocks) until a channel is ready.What is the purpose of the
default case in a select?✗ Incorrect
The
default case runs immediately if no channels are ready, preventing blocking.Which of these is a valid use of
select with multiple channels?✗ Incorrect
Option D shows correct syntax waiting to receive from multiple channels.
Explain how the
select statement works with multiple channels in Go.Think about how Go handles multiple channel operations at once.
You got /5 concepts.
Describe a real-life situation where using
select with multiple channels would be helpful.Imagine you are waiting for messages from different friends on separate phones.
You got /4 concepts.