Mahdi.Kh
November 23, 2026
We learn what a send and a receive really do, what a buffer changes, how to close a channel and who should close it, and how to steer clear of deadlocks and stranded goroutines.
In the previous part our goroutines handed their results back through the slots of a slice, and main read them once wg.Wait() returned. That works when you know how many results are coming and only need them at the end. Often you need something else: each result the moment it is ready, a stream of values whose length nobody knows in advance, or one goroutine telling the others "go" or "that's all". Goroutines need a way to talk to each other, and in Go that way is a channel.
In this part we look at what a send and a receive really do, the difference a buffer makes, how a channel's type can say who may send on it, and the question behind most channel bugs: who closes it? If the syntax itself is new to you, the Channels post of our Golang basics series introduces it gently.
A channel is a typed pipe between goroutines. ch <- v sends v into it, and v := <-ch receives the next value from it. A Go proverb sums up why that matters:
Don't communicate by sharing memory; share memory by communicating.
Instead of two goroutines reaching into the same variable and guarding it with locks, one hands the value to the other, and from then on the value is the receiver's to use.
Whether a send has to wait depends on the channel's buffer. Think of delivering a parcel. An unbuffered channel, make(chan int), is a handoff in person: the courier cannot leave until someone takes the parcel, and nobody can take one before a courier arrives. A send and its receive happen together, so a completed send tells the sender that the value has arrived. A buffered channel, make(chan int, 3), is a locker with three compartments: the courier drops the parcel and leaves while a compartment is free, and you collect it whenever you like. The sender waits only when the locker is full, and the receiver only when it is empty.
Notice what the diagram does not promise: a buffer does not make the receiver any faster. It only decides how far ahead the sender may get.
Let's see the difference in time. main hands three documents to a printer goroutine that needs 100 ms for each one, first through an unbuffered channel and then through one with room for three:
With capacity 0, main is dragged along at the printer's pace. It hands over the report at once, because the printer is already waiting for it, but the invoice only at 100 ms, when the printer has finished the report and comes back for more. Every completed send is a meeting point: when docs <- doc returns, the printer has the document.
With capacity 3, all three documents go straight into the buffer and main is back to work at 0 s, while the printer works through the buffer at its own pace. The printing still ends at about 300 ms in both runs, because the printer is the bottleneck and no buffer can change that. Send ten documents into a buffer of three and main gets three ahead, then waits at the printer's pace again. That waiting is called backpressure, and it is a feature: a full channel slows the producer down instead of letting work pile up without limit. The exact milliseconds may wobble by 10 ms or so between runs; the shape will not.
One thing in this program is unrealistic: the printer knows it will get exactly three documents. Real receivers rarely know that, which brings us to closing.
close(ch) tells the receivers that no more values will be sent. They can still take whatever is buffered; after that, every receive returns at once with the zero value. There are two ways to notice. for v := range ch receives until the channel is closed and empty, and then the loop ends. v, ok := <-ch sets ok to false when v is that zero value from a closed channel rather than a value somebody sent.
The tray still delivers both strings after it is closed, and then "" false. The second half shows something a send cannot do. A send wakes exactly one receiver, but closing wakes every receiver at once. Closing a channel that nobody sends on is the standard way to broadcast a signal, like a starting pistol, or "stop" to every worker; the ctx.Done() channel of a context is exactly that. The runners leave in a different order on every run, because once they are all released, the scheduler decides who prints first.
All the rules fit in one picture:
Three cells deserve a second look. Sending on a closed channel panics, and so does closing a channel twice. And a nil channel, the zero value of any channel type (a var ch chan int that was never given a make), blocks forever on both send and receive. That sounds useless, but in the next part we will use it on purpose, to switch off one case of a select.
So who closes? The sending side, and only once no more sends can happen. A receiver must never close, because it cannot know whether a sender is about to send, and a send on a closed channel crashes the program. With one sender, that sender closes when it is done, often with defer close(out). With several senders, none of them knows it is the last, so somebody else has to wait for all of them and close.
Let's put it all together: check several services at the same time and print each status as soon as it arrives.
This program is small, but it has the shape you will see again and again:
checkAll returns <-chan status, a receive-only channel. Inside the function, statuses is an ordinary two-way channel, and returning it converts it. A direction is part of the type: chan<- T can only be sent on and <-chan T can only be received from.WaitGroup and closes statuses after the last send. This little closer is one of the most common idioms in Go, and we will meet it again in Fan-Out/Fan-In.main does not know how many statuses will come. range ends when the channel is closed, which is exactly when the last check is done.auth and mail both take 80 ms, so the order of those two lines changes between runs.The direction in the return type is worth more than it looks. The caller can range over the channel, but the compiler stops it from doing the two things that would break the design:
Put directions on function parameters too: a worker(jobs <-chan Job, results chan<- Result) says in its signature what it reads and what it writes.
An unbuffered send waits for a receiver, and the only goroutine that could receive is the one that is waiting. The runtime sees that no goroutine can ever make progress and stops the program with the message above. Move the send, or the receive, into another goroutine. Be aware that the runtime only notices when every goroutine is stuck. In a server, where plenty of other goroutines are alive, the same mistake just leaves one goroutine blocked forever, silently.
range waits for a close that never comes. In main you get the deadlock error; anywhere else you get a goroutine that is stuck for good. Make the close impossible to forget by writing defer close(out) as the first line of the sending goroutine.
A receiver that closes, a sender that closes while other senders are still sending, two goroutines that both close: each is a panic waiting for the right timing. In your design, "who closes this channel?" should have a one-word answer. When several goroutines may need to say "stop", don't let them close a data channel; give them a separate signal channel that is closed exactly once, for example with sync.Once from the Once part, or better, use a context.
Remember fastest from the previous part? It returned with the first answer and left the other goroutines blocked forever on their sends. Now we can fix it:
With a slot for every sender, no send can block, so every goroutine finishes even though only one answer is ever read, and the garbage collector takes care of the channel and the unread answers. This is one of the few places where a buffer size is chosen for correctness rather than speed. The slower downloads still run to the end, though; stopping them early takes cancellation, which select and context will give us.
When a program deadlocks on an unbuffered channel, it is tempting to add a buffer of 100 and move on. Usually that only moves the deadlock to the 101st value, where it waits for the busiest day in production. Choose a buffer size for a reason you can say out loud: 0 when you want a handoff, the number of senders when each sends exactly one value (as in fastest), or a small number to smooth out bursts between a producer and a consumer that keep the same pace on average.
sync.Mutex is simpler and faster for that, as the Mutex and RWMutex part shows.select.range or v, ok := <-ch.<-chan T from functions that produce values, so the compiler keeps callers from sending or closing.In the next part, select lets one goroutine wait on several channels at once, and stop waiting when it has waited long enough.
Previous part: Goroutine Basics | Concurrency in Go
Next part: The select Statement | Concurrency in Go