By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Goroutines need to exchange data. Go's answer is a channel: a typed pipe that carries values from one goroutine to another. You send with an arrow pointing into the channel and receive with an arrow pointing out:
make(chan int). The type inside the brackets is the kind of value it carries.ch <- 450. This blocks until the value can be handed over.amount := <-ch. This blocks until a value arrives.close(ch) when the sender has no more values.A channel made with make(chan int) is unbuffered: it has no storage. A send blocks until a receiver is ready, and a receive blocks until a sender arrives. The two goroutines meet, hand the value over, and continue.
package main
import "fmt"
func main() {
messages := make(chan string)
go func() {
messages <- "coffee costs $4.50"
}()
message := <-messages
fmt.Println("received:", message)
}This example also shows the order rule. make(chan string) creates the channel in main, the goroutine is started, and main receives. If the goroutine sent before main reached the receive, it would wait until main arrived. Neither side can run ahead.
A buffered channel has storage. make(chan int, 3) holds up to three values, so a sender can send three times without a receiver waiting. The fourth send blocks until room appears. The table shows the difference:
| Channel | Created with | A send blocks when... | A receive blocks when... |
|---|---|---|---|
| Unbuffered | make(chan int) | no receiver is ready | no sender is ready |
| Buffered | make(chan int, 3) | the buffer is full | the buffer is empty |
This program fills a buffer, closes it, and lets range drain the rest. A closed channel returns its remaining values first, then ends the loop:
package main
import "fmt"
func main() {
amounts := make(chan int, 3)
amounts <- 450
amounts <- 275
amounts <- 1975
close(amounts)
total := 0
for amount := range amounts {
fmt.Println("received:", amount)
total += amount
}
fmt.Println("total:", total)
}make before anyone uses it.select waits on several channel operations at once and runs the first one that is ready. time.After returns a channel that receives one value after a duration, which makes a deadline easy to express. This program waits for a message, with a timeout as the backup:
package main
import (
"fmt"
"time"
)
func main() {
messages := make(chan string)
select {
case message := <-messages:
fmt.Println("received:", message)
case <-time.After(20 * time.Millisecond):
fmt.Println("timed out waiting for a message")
}
}The select has exactly one ready case here, so the result is predictable. If two cases are ready at the same moment, Go chooses one at random. That randomness is useful for load balancing and confusing for tests, so a deterministic program needs at most one ready case at a time.
A deadlock happens when every goroutine is blocked and none can make progress. The classic shape is a send with no receiver: main tries to send to an empty unbuffered channel and waits forever. Go notices and stops the program with a message like fatal error: all goroutines are asleep - deadlock!.
Change the handoff example so it sends two prices over an unbuffered channel and prints both. Then replace the second receive with a select that has a time.After(1 * time.Second) case and predict which case runs. Finally, make a buffered channel with room for one value, send twice without a receiver, and run it: the second send deadlocks. Add a receiver goroutine and watch the deadlock go away.
Key takeaways
A channel is a typed pipe. Send with ch <- value, receive with value := <-ch.
Unbuffered channels have no storage, so sender and receiver must meet. Buffered channels store values until full.
Only the sender closes a channel, and range drains a closed channel before ending.
select runs the first ready case; time.After turns a wait into a deadline.
A send with no receiver and a receive with no sender are the two classic deadlocks.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.