Channel creation in Go - Time & Space Complexity
When we create a channel in Go, we want to know how long it takes as the program runs.
We ask: Does making a channel take more time if the program is bigger or smaller?
Analyze the time complexity of the following code snippet.
package main
func main() {
ch := make(chan int)
_ = ch
}
This code creates a new channel for integers and stores it in variable ch.
Look for loops or repeated steps.
- Primary operation: Creating a single channel with
make(chan int). - How many times: Exactly once, no loops or repeats.
Creating one channel takes the same time no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time to create a channel stays the same even if the program or input size grows.
Time Complexity: O(1)
This means creating a channel takes a fixed amount of time, no matter how big the program or input is.
[X] Wrong: "Creating a channel takes longer if the program has more data or runs longer."
[OK] Correct: Creating a channel is a simple setup step that does not depend on data size or program length.
Understanding that channel creation is quick and constant helps you explain how Go manages communication efficiently.
"What if we create multiple channels inside a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand channel creation syntax
Channels are created using the built-in make function with the syntax make(chan Type).Step 2: Identify the correct option
ch := make(chan int) uses make(chan int), which is the correct syntax for an unbuffered integer channel.Final Answer:
ch := make(chan int) -> Option CQuick Check:
make(chan int) = correct channel creation [OK]
- Using new(chan int) instead of make
- Incorrect syntax like chan int{}
- Swapping order in make(int chan)
Solution
Step 1: Recall buffered channel syntax
Buffered channels are created by passing a second argument to make, which is the capacity (an integer).Step 2: Identify the correct option
ch := make(chan string, 5) correctly uses make(chan string, 5) to create a buffered string channel with capacity 5.Final Answer:
ch := make(chan string, 5) -> Option BQuick Check:
make(chan string, 5) = buffered channel [OK]
- Omitting capacity for buffered channel
- Using string instead of int for capacity
- Incorrect syntax with named parameters
ch := make(chan int, 2) ch <- 10 ch <- 20 fmt.Println(<-ch) fmt.Println(<-ch)
Solution
Step 1: Understand buffered channel behavior
The channel has capacity 2, so it can hold two values without blocking. Values 10 and 20 are sent in order.Step 2: Read values from channel in FIFO order
Receiving from the channel returns values in the order sent: first 10, then 20.Final Answer:
10 20 -> Option AQuick Check:
Buffered channel FIFO order = 10 then 20 [OK]
- Assuming output reverses order
- Expecting runtime or compile error
- Confusing buffered with unbuffered channel behavior
ch := make(chan int) ch <- 5 fmt.Println(<-ch)
Solution
Step 1: Analyze unbuffered channel send
Unbuffered channels block the sender until a receiver is ready. Here, send happens before receive.Step 2: Identify deadlock situation
Since no goroutine is receiving concurrently, the send blocks forever causing deadlock.Final Answer:
Deadlock: sending on unbuffered channel without receiver -> Option AQuick Check:
Unbuffered send blocks without receiver = deadlock [OK]
- Thinking code compiles but runs fine
- Confusing syntax error with runtime deadlock
- Assuming print causes error
Solution
Step 1: Create buffered channel with capacity 3
Use make(chan float64, 3) to create a channel that can hold 3 float64 values without blocking.Step 2: Send three values safely
Sending three values to this buffered channel will not block because capacity matches the number of sends.Final Answer:
ch := make(chan float64, 3) ch <- 1.1 ch <- 2.2 ch <- 3.3 -> Option DQuick Check:
Buffered channel capacity 3 allows 3 sends without blocking [OK]
- Using unbuffered channel for multiple sends
- Buffer smaller than sends causing deadlock
- Receiving before sending without goroutine
Ready to prove it?
Pass the test and this pattern moves unknown
