What if your program could know exactly when to stop waiting for messages without guessing?
Why Channel closing behavior in Go? - Purpose & Use Cases
Imagine you have a group chat where friends send messages. If someone suddenly leaves without saying goodbye, others might keep waiting for messages that will never come.
Without a clear way to signal that no more messages will come, your program can get stuck waiting forever, wasting time and resources. Manually checking if messages are done is tricky and error-prone.
Channels in Go can be closed to signal that no more data will be sent. This lets receivers know when to stop waiting, making communication clean and efficient.
for { msg := <-ch // no way to know if channel is closed process(msg) }
for msg := range ch {
process(msg)
}It enables safe and clear communication between parts of your program, preventing deadlocks and infinite waits.
Think of a factory assembly line where workers pass items along. When the line stops, a clear signal tells everyone to finish up and stop waiting for more items.
Closing a channel signals no more data will come.
Receivers can detect closure and stop waiting.
This prevents your program from hanging or wasting resources.
Practice
Solution
Step 1: Understand channel closing purpose
Closing a channel signals receivers that no more values will be sent.Step 2: Effects on senders and receivers
Senders cannot send after closing; receivers get zero value and ok=false.Final Answer:
Receivers know no more values will be sent -> Option CQuick Check:
close(channel) signals receivers [OK]
- Thinking senders can send after close
- Believing channel is deleted immediately
- Assuming channel blocks forever after close
ch in Go?Solution
Step 1: Recall Go syntax for closing channels
The built-in function close() is used with the channel as argument.Step 2: Identify correct syntax
Only close(ch) is valid; methods like ch.close() or ch.Close() do not exist.Final Answer:
close(ch) -> Option DQuick Check:
Use close(channel) syntax [OK]
- Trying to call close as a method
- Using incorrect keywords like 'close channel'
- Capitalizing close as Close
package main
import "fmt"
func main() {
ch := make(chan int, 2)
ch <- 10
ch <- 20
close(ch)
for v := range ch {
fmt.Print(v, " ")
}
}Solution
Step 1: Understand channel buffering and closing
Channel has buffer size 2, two values sent, then channel closed.Step 2: Loop over channel with range
Range reads all values until channel is closed and drained, printing 10 and 20.Final Answer:
10 20 -> Option AQuick Check:
Range over closed channel prints buffered values [OK]
- Expecting panic on reading closed channel
- Thinking zero value prints after buffered values
- Assuming no output because channel is closed
ch := make(chan int)
go func() {
for i := 0; i < 3; i++ {
ch <- i
}
close(ch)
}()
for {
v, ok := <-ch
if !ok {
break
}
fmt.Println(v)
}Solution
Step 1: Analyze channel usage and goroutine
Unbuffered channel with sender in goroutine sends 3 values then closes channel.Step 2: Check receiver loop behavior
Receiver reads values until channel closes, then breaks loop correctly.Final Answer:
No error, code runs and prints 0 1 2 -> Option BQuick Check:
Proper close and ok check ends loop [OK]
- Thinking closing in goroutine causes panic
- Assuming infinite loop without break
- Ignoring need to check ok when receiving
package main
import "fmt"
func main() {
ch := make(chan int)
go func() {
for i := 1; i <= 3; i++ {
ch <- i
}
close(ch)
}()
for {
v, ok := <-ch
if !ok {
break
}
if v%2 == 0 {
fmt.Println(v)
}
}
}
What is the output and why?Solution
Step 1: Understand sending and closing in goroutine
Values 1, 2, 3 sent then channel closed properly.Step 2: Receiver loop filters even numbers
Loop reads until closed; prints only values where v%2 == 0, which is 2.Final Answer:
2 Only even numbers printed before channel closes -> Option AQuick Check:
Filter with condition inside loop after close [OK]
- Expecting all values printed
- Thinking channel closes before sending
- Assuming panic on close with goroutine sender
Ready to prove it?
Pass the test and this pattern moves unknown
