Sending and receiving values in Go - Time & Space Complexity
When we send and receive values in Go channels, we want to know how the time to do this changes as we handle more data.
We ask: How does the number of send and receive actions grow with the input size?
Analyze the time complexity of the following code snippet.
package main
func main() {
ch := make(chan int)
go func() {
for i := 0; i < 5; i++ {
ch <- i
}
close(ch)
}()
for val := range ch {
_ = val
}
}
This code sends 5 values into a channel and receives them in the main goroutine.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending and receiving values in a loop.
- How many times: 5 times each for sending and receiving.
As the number of values to send and receive grows, the total operations grow in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sends + 10 receives = 20 |
| 100 | 100 sends + 100 receives = 200 |
| 1000 | 1000 sends + 1000 receives = 2000 |
Pattern observation: The total work grows directly with the number of values sent and received.
Time Complexity: O(n)
This means the time to send and receive values grows in a straight line as the number of values increases.
[X] Wrong: "Sending and receiving values in channels is constant time no matter how many values there are."
[OK] Correct: Each value must be sent and received separately, so the total time grows with the number of values.
Understanding how sending and receiving values scale helps you reason about concurrent programs and their performance in real projects.
"What if we used buffered channels with capacity n? How would the time complexity change?"
Practice
5 to a channel named ch in Go?Solution
Step 1: Understand channel send syntax
In Go, to send a value to a channel, you writechannel <- value.Step 2: Apply syntax to given variables
Here, the channel ischand the value is5, so the correct send isch <- 5.Final Answer:
ch <- 5 -> Option AQuick Check:
Send syntax = channel <- value [OK]
- Reversing send syntax as value <- channel
- Using assignment operator instead of send
- Calling a non-existent send function
ch and store it in variable v?Solution
Step 1: Recall channel receive syntax
To receive a value from a channel, Go usesvalue := <- channel.Step 2: Match syntax with variables
Here,vreceives fromch, sov := <- chis correct.Final Answer:
v := <- ch -> Option DQuick Check:
Receive syntax = v := <- ch [OK]
- Placing <- after channel name
- Using method calls like receive()
- Using assignment = instead of := for declaration
package main
import "fmt"
func main() {
ch := make(chan int)
go func() {
ch <- 10
}()
val := <-ch
fmt.Println(val)
}Solution
Step 1: Understand channel creation and goroutine
The channelchis created and a goroutine sends10to it.Step 2: Receive value and print
The main goroutine receives the value fromchand prints it.Final Answer:
10 -> Option CQuick Check:
Send and receive work correctly, output = 10 [OK]
- Expecting zero value instead of sent value
- Thinking channel blocks forever causing deadlock
- Confusing send and receive syntax
package main
func main() {
ch := make(chan int)
ch <- 5
val := <- ch
println(val)
}Solution
Step 1: Analyze channel usage without goroutine
The code sends tochbut no goroutine is receiving yet, so send blocks forever.Step 2: Understand deadlock cause
Since the send blocks and main goroutine waits, program deadlocks before receiving.Final Answer:
Sending to channel blocks forever causing deadlock -> Option BQuick Check:
Send blocks without concurrent receiver [OK]
- Assuming send works without concurrent receive
- Thinking channel creation is wrong
- Confusing syntax errors with runtime deadlock
1, 2, 3 to a channel ch and receive them in order. Which code snippet correctly does this without causing deadlock?Solution
Step 1: Check for deadlock prevention
ch := make(chan int) go func() { ch <- 1 ch <- 2 ch <- 3 }() for i := 0; i < 3; i++ { fmt.Println(<-ch) }uses a goroutine to send values, so sending won't block the main goroutine.Step 2: Confirm receiving order and printing
Main goroutine receives three values in order and prints them correctly.Final Answer:
This code runs without deadlock and prints 1, 2, 3 -> Option AQuick Check:
Use goroutine to send multiple values safely [OK]
- Sending multiple values on unbuffered channel without goroutine
- Assuming unbuffered channel allows multiple sends without receive
- Forgetting to import fmt for printing
Ready to prove it?
Pass the test and this pattern moves unknown
