0
0
Goprogramming~20 mins

Why select is needed in Go - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Select Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Go program using select?

Consider this Go program that uses select to wait on multiple channels. What will it print?

Go
package main

import (
	"fmt"
	"time"
)

func main() {
	ch1 := make(chan string)
	ch2 := make(chan string)

	go func() {
		time.Sleep(100 * time.Millisecond)
		ch1 <- "from ch1"
	}()

	go func() {
		time.Sleep(50 * time.Millisecond)
		ch2 <- "from ch2"
	}()

	select {
	case msg1 := <-ch1:
		fmt.Println(msg1)
	case msg2 := <-ch2:
		fmt.Println(msg2)
	}
}
Afrom ch1
BProgram deadlocks
CCompilation error
Dfrom ch2
Attempts:
2 left
💡 Hint

Think about which goroutine sends data first and how select picks the ready channel.

🧠 Conceptual
intermediate
1:30remaining
Why is select needed in Go concurrency?

Which of the following best explains why the select statement is needed in Go?

ATo lock multiple channels at once to prevent race conditions.
BTo create new goroutines automatically without explicit calls.
CTo allow a goroutine to wait on multiple channel operations simultaneously and proceed with the one that is ready first.
DTo convert channels into buffered queues.
Attempts:
2 left
💡 Hint

Think about how Go handles multiple channel communications at the same time.

🔧 Debug
advanced
2:00remaining
What error does this Go code cause and why?

Examine this Go code snippet using select. What error will it produce when run?

Go
package main

func main() {
	ch := make(chan int)

	select {
	case val := <-ch:
		println(val)
	}
}
ADeadlock error because no goroutine sends data to the channel.
BCompilation error due to missing default case in select.
CRuntime panic due to nil channel.
DNo error; program prints 0 and exits.
Attempts:
2 left
💡 Hint

Think about what happens when a goroutine waits to receive from a channel with no sender.

Predict Output
advanced
2:00remaining
What is the output of this Go program with multiple select cases?

What will this Go program print?

Go
package main

import (
	"fmt"
	"time"
)

func main() {
	ch1 := make(chan string)
	ch2 := make(chan string)

	go func() {
		ch1 <- "hello"
	}()

	go func() {
		ch2 <- "world"
	}()

	select {
	case msg := <-ch1:
		fmt.Println(msg)
	case msg := <-ch2:
		fmt.Println(msg)
	default:
		fmt.Println("no message received")
	}
}
Aworld
Bno message received
CProgram deadlocks
Dhello
Attempts:
2 left
💡 Hint

Consider the timing of goroutines and the default case in select.

🧠 Conceptual
expert
2:30remaining
Why does Go's select statement improve concurrency control?

Which statement best describes how Go's select improves concurrency control compared to blocking on a single channel?

A<p><code>select</code> enables a goroutine to handle multiple communication events without blocking indefinitely on one, improving responsiveness and avoiding deadlocks.</p>
B<p><code>select</code> automatically balances load between goroutines by redistributing channel messages.</p>
C<p><code>select</code> serializes all channel operations to ensure strict ordering of messages.</p>
D<p><code>select</code> replaces mutexes by locking channels during communication.</p>
Attempts:
2 left
💡 Hint

Think about how waiting on multiple channels helps a goroutine stay active.