Challenge - 5 Problems
Go Select Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of select with two channels
What is the output of this Go program?
Go
package main import ( "fmt" "time" ) func main() { ch1 := make(chan string) ch2 := make(chan string) go func() { time.Sleep(100 * time.Millisecond) ch1 <- "channel 1" }() go func() { time.Sleep(50 * time.Millisecond) ch2 <- "channel 2" }() select { case msg1 := <-ch1: fmt.Println(msg1) case msg2 := <-ch2: fmt.Println(msg2) } }
Attempts:
2 left
💡 Hint
Remember that select picks the first channel ready to send or receive.
✗ Incorrect
The goroutine sending to ch2 sleeps less (50ms) than the one sending to ch1 (100ms). So ch2 is ready first, and select receives from ch2, printing "channel 2".
❓ Predict Output
intermediate2:00remaining
Select with default case output
What will this Go program print?
Go
package main import "fmt" func main() { ch := make(chan int) select { case val := <-ch: fmt.Println(val) default: fmt.Println("no value received") } }
Attempts:
2 left
💡 Hint
The channel is empty and no goroutine sends to it.
✗ Incorrect
Since ch has no value and no goroutine sends to it, the receive case blocks. The default case runs immediately, printing "no value received".
🔧 Debug
advanced2:00remaining
Why does this select cause deadlock?
This Go program causes a deadlock. Why?
Go
package main func main() { ch1 := make(chan int) ch2 := make(chan int) select { case <-ch1: // do nothing case <-ch2: // do nothing } }
Attempts:
2 left
💡 Hint
Think about what happens when select waits on channels with no senders.
✗ Incorrect
Both channels are unbuffered and no goroutine sends to them, so the select waits forever for a value that never comes, causing a deadlock.
❓ Predict Output
advanced2:00remaining
Select with multiple ready channels
What output does this Go program produce?
Go
package main import ( "fmt" ) func main() { ch1 := make(chan string, 1) ch2 := make(chan string, 1) ch1 <- "first" ch2 <- "second" select { case msg := <-ch1: fmt.Println(msg) case msg := <-ch2: fmt.Println(msg) } }
Attempts:
2 left
💡 Hint
When multiple channels are ready, select picks one at random but Go's implementation picks the first case in source order.
✗ Incorrect
Both channels have buffered values ready. Select picks one randomly, but Go's implementation picks the first ready case in source order, so it prints "first".
❓ Predict Output
expert2:00remaining
Select with timeout using time.After
What is the output of this Go program?
Go
package main import ( "fmt" "time" ) func main() { ch := make(chan string) select { case msg := <-ch: fmt.Println(msg) case <-time.After(100 * time.Millisecond): fmt.Println("timeout") } }
Attempts:
2 left
💡 Hint
The channel ch never receives a value, but time.After sends a value after 100ms.
✗ Incorrect
The channel ch never receives any value, so the receive case blocks. The time.After channel sends a value after 100ms, so the select unblocks and prints "timeout".