Closing a channel means no more values can be sent on it. Receivers can still receive remaining values until the channel is empty, then they get the zero value.
Click to reveal answer
beginner
Can you send values on a closed channel?
No. Sending on a closed channel causes a runtime panic (error).
Click to reveal answer
intermediate
What does a receive operation return when the channel is closed and empty?
It returns the zero value of the channel's type and false for the second value if using the two-value receive form.
Click to reveal answer
intermediate
How can you check if a channel is closed when receiving?
Use the two-value receive: v, ok := <-ch. If ok is false, the channel is closed and empty.
Click to reveal answer
intermediate
Is it safe to close a channel from multiple goroutines?
No. Closing a channel multiple times causes a panic. Only one goroutine should close the channel.
Click to reveal answer
What happens if you try to send a value on a closed channel?
AThe program panics (runtime error).
BThe value is silently ignored.
CThe value is buffered until the channel reopens.
DThe send blocks forever.
✗ Incorrect
Sending on a closed channel causes a runtime panic in Go.
When receiving from a closed channel, what value do you get?
AThe receive blocks forever.
BThe last sent value again.
CAn error is returned.
DThe zero value of the channel's type.
✗ Incorrect
After a channel is closed and emptied, receives return the zero value of the channel's type.
How do you detect if a channel is closed when receiving?
AUse a select statement with default case.
BUse the two-value receive: v, ok := <-ch.
CCheck if the channel is nil.
DYou cannot detect if a channel is closed.
✗ Incorrect
The two-value receive form returns ok=false when the channel is closed and empty.
Who should close a channel?
AAny goroutine at any time.
BThe receiver, when done receiving.
CThe sender, when no more values will be sent.
DChannels do not need to be closed.
✗ Incorrect
The sender should close the channel to signal no more values will be sent.
What happens if multiple goroutines close the same channel?
AThe program panics at the second close.
BThe channel closes safely once.
CThe channel reopens automatically.
DThe closes are ignored after the first.
✗ Incorrect
Closing a channel more than once causes a panic.
Explain what happens when a Go channel is closed and how receivers behave afterwards.
Think about what sending and receiving do after closing.
You got /4 concepts.
Describe best practices for closing channels in concurrent Go programs.
Who should close and why?
You got /4 concepts.
Practice
(1/5)
1. What happens when you close a channel in Go?
easy
A. The channel is deleted from memory immediately
B. Senders can still send values without error
C. Receivers know no more values will be sent
D. The channel blocks all operations permanently
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 C
Quick Check:
close(channel) signals receivers [OK]
Hint: Closing signals receivers no more data comes [OK]
Common Mistakes:
Thinking senders can send after close
Believing channel is deleted immediately
Assuming channel blocks forever after close
2. Which of the following is the correct way to close a channel named ch in Go?
easy
A. ch.Close()
B. ch.close()
C. close channel ch
D. close(ch)
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 D
Quick Check:
Use close(channel) syntax [OK]
Hint: Use built-in close(channel) function [OK]
Common Mistakes:
Trying to call close as a method
Using incorrect keywords like 'close channel'
Capitalizing close as Close
3. What is the output of this Go program?
package main
import "fmt"
func main() {
ch := make(chan int, 2)
ch <- 10
ch <- 20
close(ch)
for v := range ch {
fmt.Print(v, " ")
}
}
medium
A. 10 20
B. 10 20 0
C. Runtime panic: send on closed channel
D. No output
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 A
Quick Check:
Range over closed channel prints buffered values [OK]
Hint: Range prints all buffered values before stopping [OK]
Common Mistakes:
Expecting panic on reading closed channel
Thinking zero value prints after buffered values
Assuming no output because channel is closed
4. Identify the error in this Go code snippet:
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)
}
medium
A. Channel is unbuffered but sender is in goroutine, no deadlock
B. No error, code runs and prints 0 1 2
C. Closing channel inside goroutine causes panic
D. Receiver loop never ends, causing infinite loop
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 B
Quick Check:
Proper close and ok check ends loop [OK]
Hint: Use ok to detect closed channel and break loop [OK]
Common Mistakes:
Thinking closing in goroutine causes panic
Assuming infinite loop without break
Ignoring need to check ok when receiving
5. Consider this Go program:
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?
hard
A. 2
Only even numbers printed before channel closes
B. 1
2
3
All numbers printed despite channel closing
C. No output because channel is closed before reading
D. Runtime panic due to sending on closed channel
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 A
Quick Check:
Filter with condition inside loop after close [OK]
Hint: Filter values after reading until channel closes [OK]
Common Mistakes:
Expecting all values printed
Thinking channel closes before sending
Assuming panic on close with goroutine sender
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.