0
0
Goprogramming~10 mins

Channel synchronization in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Channel synchronization
Start main goroutine
Create channel
Start goroutine to send data
Send data into channel
Main goroutine waits to receive
Receive data from channel
Print received data
Program ends
The main goroutine creates a channel and starts another goroutine that sends data into it. The main goroutine waits to receive data from the channel, synchronizing the two goroutines.
Execution Sample
Go
package main
import "fmt"
func main() {
  ch := make(chan string)
  go func() { ch <- "hello" }()
  msg := <-ch
  fmt.Println(msg)
}
This Go program uses a channel to synchronize sending and receiving a string between two goroutines.
Execution Table
StepActionChannel StateGoroutine StateOutput
1Create channel chemptymain running
2Start goroutine to send "hello"emptymain and sender running
3Sender tries to send "hello" into chblocked (waiting for receiver)sender blocked
4Main goroutine waits to receive from chblocked (waiting for sender)main blocked
5Sender sends "hello" into chcontains "hello"sender unblocked, ends
6Main receives "hello" from chemptymain unblocked
7Print "hello"emptymain runninghello
8Program endsemptyall goroutines ended
💡 Program ends after main goroutine prints the received message and all goroutines finish.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6Final
chemptyblocked (send waiting)contains "hello"emptyempty
msgundefinedundefinedundefined"hello""hello"
Key Moments - 3 Insights
Why does the sender goroutine block when sending to the channel?
Because the channel is unbuffered, the sender waits until the main goroutine receives the value, as shown in step 3 of the execution_table.
How does the main goroutine know when to continue after waiting on the channel?
It continues after receiving the value from the channel, which unblocks it as shown in step 6 of the execution_table.
What happens if the main goroutine tries to receive before the sender sends?
The main goroutine blocks and waits until the sender sends a value, ensuring synchronization, as seen in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the sender goroutine unblock?
AStep 5
BStep 6
CStep 3
DStep 7
💡 Hint
Check the 'Goroutine State' column for the sender goroutine status.
According to variable_tracker, what is the state of channel 'ch' after step 6?
Acontains "hello"
Bempty
Cblocked (send waiting)
Dclosed
💡 Hint
Look at the 'ch' row under 'After Step 6' in variable_tracker.
If the channel was buffered with capacity 1, how would the sender's state at step 3 change?
ASender would never send
BSender would block waiting for receiver
CSender would send immediately without blocking
DSender would panic
💡 Hint
Buffered channels allow sending without immediate receiver if buffer space is available.
Concept Snapshot
Channel synchronization in Go:
- Create channel with make(chan Type)
- Sending blocks until receiver is ready (unbuffered channel)
- Receiving blocks until sender sends
- Synchronizes goroutines by waiting on send/receive
- Use channels to safely share data between goroutines
Full Transcript
This example shows how Go uses channels to synchronize two goroutines. The main goroutine creates an unbuffered channel and starts a sender goroutine. The sender tries to send a string into the channel but blocks because the main goroutine has not yet received it. The main goroutine waits to receive from the channel, which unblocks the sender. After the sender sends the string, the main goroutine receives it and prints it. This synchronization ensures safe communication between goroutines without race conditions.