Sending and receiving values lets different parts of a Go program talk to each other safely and clearly.
Sending and receiving values in Go
channel <- value // send value to channel value := <-channel // receive value from channel
Channels are like pipes that let you send and receive values between goroutines.
Use channel <- value to send and value := <- channel to receive.
ch := make(chan int) // Sending ch <- 5 // Receiving val := <-ch
ch := make(chan string)
go func() {
ch <- "hello"
}()
msg := <-chThis program creates a channel for strings. A new goroutine sends a message through the channel. The main function waits to receive the message and then prints it.
package main import "fmt" func main() { ch := make(chan string) go func() { ch <- "Hi from goroutine" }() msg := <-ch fmt.Println(msg) }
Always make channels before using them with make(chan Type).
Receiving from a channel will wait until there is a value to get.
Sending to a channel will wait until someone is ready to receive, unless the channel is buffered.
Channels let goroutines send and receive values safely.
Use channel <- value to send and value := <- channel to receive.
This helps your program parts work together without confusion.
Practice
5 to a channel named ch in Go?Solution
Step 1: Understand channel send syntax
In Go, to send a value to a channel, you writechannel <- value.Step 2: Apply syntax to given variables
Here, the channel ischand the value is5, so the correct send isch <- 5.Final Answer:
ch <- 5 -> Option AQuick Check:
Send syntax = channel <- value [OK]
- Reversing send syntax as value <- channel
- Using assignment operator instead of send
- Calling a non-existent send function
ch and store it in variable v?Solution
Step 1: Recall channel receive syntax
To receive a value from a channel, Go usesvalue := <- channel.Step 2: Match syntax with variables
Here,vreceives fromch, sov := <- chis correct.Final Answer:
v := <- ch -> Option DQuick Check:
Receive syntax = v := <- ch [OK]
- Placing <- after channel name
- Using method calls like receive()
- Using assignment = instead of := for declaration
package main
import "fmt"
func main() {
ch := make(chan int)
go func() {
ch <- 10
}()
val := <-ch
fmt.Println(val)
}Solution
Step 1: Understand channel creation and goroutine
The channelchis created and a goroutine sends10to it.Step 2: Receive value and print
The main goroutine receives the value fromchand prints it.Final Answer:
10 -> Option CQuick Check:
Send and receive work correctly, output = 10 [OK]
- Expecting zero value instead of sent value
- Thinking channel blocks forever causing deadlock
- Confusing send and receive syntax
package main
func main() {
ch := make(chan int)
ch <- 5
val := <- ch
println(val)
}Solution
Step 1: Analyze channel usage without goroutine
The code sends tochbut no goroutine is receiving yet, so send blocks forever.Step 2: Understand deadlock cause
Since the send blocks and main goroutine waits, program deadlocks before receiving.Final Answer:
Sending to channel blocks forever causing deadlock -> Option BQuick Check:
Send blocks without concurrent receiver [OK]
- Assuming send works without concurrent receive
- Thinking channel creation is wrong
- Confusing syntax errors with runtime deadlock
1, 2, 3 to a channel ch and receive them in order. Which code snippet correctly does this without causing deadlock?Solution
Step 1: Check for deadlock prevention
ch := make(chan int) go func() { ch <- 1 ch <- 2 ch <- 3 }() for i := 0; i < 3; i++ { fmt.Println(<-ch) }uses a goroutine to send values, so sending won't block the main goroutine.Step 2: Confirm receiving order and printing
Main goroutine receives three values in order and prints them correctly.Final Answer:
This code runs without deadlock and prints 1, 2, 3 -> Option AQuick Check:
Use goroutine to send multiple values safely [OK]
- Sending multiple values on unbuffered channel without goroutine
- Assuming unbuffered channel allows multiple sends without receive
- Forgetting to import fmt for printing
Ready to prove it?
Pass the test and this pattern moves unknown
