0
0
Goprogramming~10 mins

Channel closing behavior in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Channel closing behavior
Create channel
Send values to channel
Close channel
Receive values
Check if channel open?
NoReceive zero value
Yes
Process value
Back to Receive values
This flow shows how a Go channel is created, values are sent, then the channel is closed. Receivers keep getting values until the channel is closed, then they receive zero values.
Execution Sample
Go
ch := make(chan int, 2)
ch <- 1
ch <- 2
close(ch)
for v := range ch {
    fmt.Println(v)
}
This code creates a channel, sends two values, closes it, then reads all values until the channel is empty.
Execution Table
StepActionChannel StateValue ReceivedChannel Closed?Output
1Create channelemptyN/ANo
2Send 1[1]N/ANo
3Send 2[1,2]N/ANo
4Close channel[1,2]N/AYes
5Receive value[2]1YesPrint 1
6Receive value[]2YesPrint 2
7Receive value[]0 (zero value)YesLoop ends
8Exit loop[]N/AYesEnd
9Program ends[]N/AYes
💡 Channel is closed and empty, so range loop ends after receiving all values.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
ch bufferempty[1][1,2][1,2] (closed)[2][][][]
value receivedN/AN/AN/AN/A120 (zero value)N/A
channel closed?NoNoNoYesYesYesYesYes
Key Moments - 3 Insights
Why does the loop stop after receiving the zero value?
The loop stops because the channel is closed and empty. Receiving from a closed channel returns the zero value, signaling no more data. See execution_table row 7 and 8.
What happens if you try to send to a closed channel?
Sending to a closed channel causes a runtime panic. This example closes the channel only after sending all values, so no panic occurs.
Why does the receiver get zero value after channel is closed?
When a channel is closed and empty, receiving returns the zero value of the channel's type immediately. This is shown in execution_table row 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value received at Step 6?
A1
B0 (zero value)
C2
DN/A
💡 Hint
Check the 'Value Received' column at Step 6 in the execution_table.
At which step does the channel get closed?
AStep 4
BStep 3
CStep 5
DStep 7
💡 Hint
Look at the 'Action' and 'Channel Closed?' columns in the execution_table.
If the channel was not closed, what would happen to the loop?
AIt would stop after receiving all values
BIt would block waiting for more values
CIt would panic
DIt would receive zero values repeatedly
💡 Hint
Refer to the concept_flow and how range over channel behaves when channel is open.
Concept Snapshot
Go channels can be closed to signal no more values.
Receiving from a closed channel returns remaining values, then zero values.
Range loops over channels stop when channel is closed and empty.
Sending to a closed channel causes panic.
Use close() carefully after all sends.
Full Transcript
This example shows how a Go channel is created, values are sent, then the channel is closed. The receiver uses a range loop to get all values until the channel is empty and closed. After that, receiving returns zero values and the loop ends. Sending to a closed channel would cause a panic, so close is called only after sending all values. The execution table traces each step: channel creation, sends, close, receives, and loop exit. The variable tracker shows the channel buffer and received values changing over time. Key moments clarify why the loop ends and what zero values mean. The quiz tests understanding of channel closing and receiving behavior.