Go channels let goroutines send data to each other safely, without sharing variables. In this post, you will learn to send and receive values, use buffered channels, close them and wait on several at once. Each idea comes with a short example that you can run yourself.
Channels connect goroutines, so first read goroutines in Go for beginners if you have not already. We will also loop over a channel with range, just like you did in loops in Go with range, break and continue.
What Are Go Channels?
A channel is a typed pipe. One goroutine puts a value in, and another takes it out on the other side. The Go team sums it up well: share memory by communicating, instead of communicating by sharing memory.
Think of the order window in a restaurant. The cook places a ready dish on the counter, and the waiter picks it up. Neither needs to walk into the other’s space. Similarly, Go channels hand data across without any shared variables.
Sending and Receiving on Go Channels
ch := make(chan string)
go func() {
ch <- "order ready" // send
}()
msg := <-ch // receive
fmt.Println(msg) // order ready
Create a channel with make(chan Type). The arrow <- shows which way data flows: ch <- v sends, while <-ch receives. Also, this channel is unbuffered, so the sender waits until a receiver is ready.

An unbuffered channel hands each value directly from sender to receiver, while a buffered one can hold a few values.
Because the receive waits for the send, the channel also works as a signal. So in this example, main cannot finish before the goroutine sends its message.
Buffered Go Channels
jobs := make(chan int, 3)
jobs <- 1
jobs <- 2
fmt.Println(len(jobs), cap(jobs)) // 2 3
Buffered Go channels have room for a fixed number of values. Sends only wait when the buffer is full, and receives only wait when it is empty. In short, a buffer lets a fast sender run a little ahead of a slow receiver.
Closing a Channel and Using range
close(jobs)
for j := range jobs {
fmt.Println("job", j)
}
// job 1
// job 2
The sender calls close to say “no more values are coming”. Then range reads every remaining value and stops on its own. Only the sender should close a channel, never the receiver.
Checking if a Channel Is Closed
v, ok := <-jobs
fmt.Println(v, ok) // 0 false
Receiving from a closed, empty channel returns the zero value at once. The second value, ok, is false in that case. This is the same comma ok idea you met with maps.
A Real Go Channels Example: Producer and Consumer
results := make(chan int)
go func() {
for i := 1; i <= 3; i++ {
results <- i * i
}
close(results)
}()
for r := range results {
fmt.Println(r)
}
// 1
// 4
// 9
The goroutine produces square numbers, and main consumes them. Once the producer closes the channel, the loop ends cleanly. Real programs use this pattern for job queues, log processing and data pipelines.
Waiting on Many Channels with select
fast := make(chan string)
go func() {
time.Sleep(10 * time.Millisecond)
fast <- "done"
}()
select {
case m := <-fast:
fmt.Println(m) // done
case <-time.After(time.Second):
fmt.Println("timeout")
}
A select waits on several channels and runs whichever case is ready first. Here, it also adds a timeout, so your program never waits forever for a slow task.
Common Mistakes with Go Channels
| Error you see | Why it happens | How to fix it |
|---|---|---|
| fatal error: all goroutines are asleep – deadlock! | A send has no receiver. | Receive in another goroutine, or add a buffer. |
| panic: send on closed channel | The code sent after close. | Close only when sending is finished. |
| Loop never ends | The sender forgot to close. | Call close(ch) after the last send. |
The deadlock error looks scary, but Go is helping you. It means every goroutine is stuck waiting. Channel puzzles are common in interviews, so try my Go interview questions and answers.
Official Resources
These pages come straight from the Go team, so they are always accurate and up to date.
Quick Recap
- Go channels pass values safely between goroutines.
- Use ch <- v to send and v := <-ch to receive.
- Unbuffered channels wait for both sides, and buffered ones hold a few values.
- The sender closes the channel, and range stops after the last value.
- Use select to wait on many channels or add a timeout.
What’s Next?
Channels are one way to coordinate goroutines. Next, we will learn the other way: locks and helpers from the sync package.
Coming soon: Go Sync Package: WaitGroup and Mutex Explained


