Default case in select in Go - Time & Space Complexity
We want to understand how the time it takes to run a select statement with a default case changes as the program runs.
Specifically, how does adding a default case affect the waiting and checking in select?
Analyze the time complexity of the following code snippet.
select {
case msg := <-ch1:
fmt.Println(msg)
case ch2 <- 1:
fmt.Println("sent")
default:
fmt.Println("no communication")
}
This code tries to receive from ch1 or send to ch2, but if neither is ready, it runs the default case immediately.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The select statement checks channel readiness once per execution.
- How many times: Each select runs once per call, no waiting if default triggers.
Since select with default does not wait, the time per select is constant regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The time grows linearly with the number of select executions, but each select is a quick check.
Time Complexity: O(n)
This means the total time grows directly with how many times the select runs, but each run is fast because it never blocks.
[X] Wrong: "The default case makes select run instantly no matter what, so time is always constant."
[OK] Correct: While each select runs quickly, if you run select many times, total time still grows with the number of runs.
Understanding how select with default behaves helps you write responsive programs that don't block, a useful skill in concurrent programming.
What if we removed the default case? How would the time complexity change when channels are not ready?
Practice
What does the default case do in a Go select statement?
Solution
Step 1: Understand the role of
Theselectselectstatement waits on multiple channel operations.Step 2: Identify the purpose of
Thedefaultdefaultcase runs immediately if no channels are ready, preventing blocking.Final Answer:
Runs immediately if no other channel operations are ready -> Option CQuick Check:
defaultruns when no channels ready = A [OK]
- Thinking default blocks until a channel is ready
- Assuming default closes channels
- Believing default sends data to all channels
Which of the following is the correct syntax for using a default case in a Go select?
select {
case msg := <-ch1:
fmt.Println(msg)
// What goes here?
}Solution
Step 1: Recall Go
Theselectsyntaxdefaultkeyword is used for the fallback case inselect.Step 2: Match correct keyword
Onlydefaultis valid;else,fallback, andotherwiseare invalid in Goselect.Final Answer:
default: fmt.Println("No message received") -> Option AQuick Check:
Usedefaultkeyword in select = C [OK]
- Using else instead of default
- Trying fallback or otherwise keywords
- Omitting the colon after default
What will be the output of this Go program?
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan string)
select {
case msg := <-ch:
fmt.Println(msg)
default:
fmt.Println("No message received")
}
}Solution
Step 1: Analyze channel state
The channelchis created but no data is sent, so it's empty and no receive is ready.Step 2: Understand select with default
Since no data is ready onch, thedefaultcase runs immediately, printing "No message received".Final Answer:
No message received -> Option DQuick Check:
No data ready triggers default = A [OK]
- Expecting program to block waiting for channel
- Thinking empty string is printed
- Assuming compilation error due to empty channel
Identify the error in this Go code snippet using select with a default case:
select {
case msg := <-ch:
fmt.Println(msg)
default
fmt.Println("No message")
}Solution
Step 1: Check
Thedefaultsyntaxdefaultcase requires a colon (:) after the keyword.Step 2: Identify missing colon
The code hasdefaultwithout a colon, causing a syntax error.Final Answer:
Missing colon after default keyword -> Option BQuick Check:
Default needs colon (:) = D [OK]
- Forgetting colon after default
- Assuming default must be last case
- Confusing variable scope in select
Consider this Go program snippet:
ch1 := make(chan int)
ch2 := make(chan int)
go func() {
time.Sleep(100 * time.Millisecond)
ch1 <- 1
}()
go func() {
time.Sleep(200 * time.Millisecond)
ch2 <- 2
}()
select {
case val := <-ch1:
fmt.Println("Received from ch1:", val)
case val := <-ch2:
fmt.Println("Received from ch2:", val)
default:
fmt.Println("No channels ready")
}What is the most likely output when this select runs?
Solution
Step 1: Understand goroutine timing
The goroutines send to channels after 100ms and 200ms delays, but theselectruns immediately.Step 2: Check channel readiness at select
At the momentselectruns, neither channel has data ready, so no case is ready.Step 3: Default case triggers
Because no channels are ready, thedefaultcase runs, printing "No channels ready".Final Answer:
No channels ready -> Option AQuick Check:
Default runs if no channel ready = B [OK]
- Assuming select waits for channels
- Expecting output from ch1 or ch2 immediately
- Thinking program deadlocks without default
Ready to prove it?
Pass the test and this pattern moves unknown
