0
0
Goprogramming~10 mins

Sending and receiving values in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sending and receiving values
Start main goroutine
Create channel
Start sender goroutine
Sender sends value into channel
Main goroutine receives value
Print received value
Program ends
This flow shows how a value is sent from one goroutine through a channel and received by another goroutine.
Execution Sample
Go
package main
import "fmt"
func main() {
  ch := make(chan int)
  go func() { ch <- 42 }()
  val := <-ch
  fmt.Println(val)
}
This Go program sends the number 42 through a channel from a goroutine and receives it in main, then prints it.
Execution Table
StepActionChannel StateValue Sent/ReceivedOutput
1Create channel chemptynone
2Start goroutine to send 42emptynone
3Goroutine sends 42 into chemptysent 42
4Main receives from chemptyreceived 42
5Print received valueempty4242
6Program endsemptynone
💡 Program ends after printing the received value 42.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
chchannel created (empty)emptyemptyempty
valundefinedundefined4242
Key Moments - 3 Insights
Why does the main goroutine wait at val := <-ch?
Because the channel is empty initially, main waits until the goroutine sends a value (see execution_table step 4).
What happens if we try to receive from a channel before any value is sent?
The receive operation blocks and waits until a value is sent, as shown in step 4 where main waits for the goroutine to send 42.
Why do we use a goroutine to send the value?
Because sending on an unbuffered channel blocks until another goroutine receives, so we start a goroutine to avoid deadlock (step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is inside the channel?
A42
Bempty
Cundefined
Dnil
💡 Hint
Check the 'Channel State' column at step 3 in the execution_table.
At which step does the main goroutine receive the value from the channel?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in the execution_table for when main receives from ch.
If we remove the goroutine and send directly in main, what happens?
AProgram deadlocks and never prints
BProgram runs and prints 42
CProgram panics with error
DProgram prints zero value
💡 Hint
Recall that sending on an unbuffered channel blocks until a receiver is ready (see key_moments about blocking).
Concept Snapshot
Go channels let goroutines send and receive values.
Use make(chan Type) to create a channel.
Send with ch <- value, receive with val := <-ch.
Sending or receiving blocks until the other side is ready.
Use goroutines to avoid deadlocks when sending and receiving.
Full Transcript
This example shows how to send and receive values using Go channels. First, a channel is created. Then a goroutine sends the value 42 into the channel. The main goroutine waits to receive this value from the channel. Once received, it prints the value 42. The program ends after printing. The key point is that sending and receiving on an unbuffered channel block until the other side is ready, so we use a goroutine to send the value to avoid deadlock.