Multiplexing in Go means using one select loop to read from, or write to, many channels so that a single goroutine coordinates several concurrent activities. It synchronizes goroutines without locks: each channel carries events or data, and select reacts to whichever event happens first.
This article covers the practical patterns built on that idea: fan-in, a shared quit signal, periodic work with a ticker, and request and response multiplexing.
A goroutine that does <-ch1 and then <-ch2 is stuck on ch1 even if ch2 is ready. A select removes that ordering problem: it blocks on all cases together and runs the one that can proceed. If several are ready, one is picked at random, so no channel is starved.
Because the loop owns the state it touches, you can often avoid a mutex altogether. This is the Go idea of sharing memory by communicating.
Fan-in combines several input channels into a single output channel. With a fixed, small number of inputs, a single select does it:
The exact order depends on timing. Setting a closed channel to nil stops select from picking it again, and the loop ends when both are nil.
For a dynamic number of inputs, start one goroutine per input that forwards to a shared channel and close the output with a sync.WaitGroup:
This version needs the sync import and Go 1.22 or later, where each loop iteration has its own in variable. On earlier versions, pass in as a function argument.
Closing a channel wakes every goroutine that is waiting on it, which makes close(done) a broadcast. Each worker listens for it in its select.
In production code, replace the hand-made done channel with ctx.Done() from a context.Context, which also carries deadlines and cancellation causes.
Another common pattern is a single goroutine that owns some state and serves requests from several channels, so no lock is needed.
Callers send on inc or send a reply channel on get. Only the owner goroutine touches n, so there is no data race. Add a done case so the goroutine can exit.
| Need | Best tool |
|---|---|
| Wait for the first of several events | select |
| Wait for all goroutines to finish | sync.WaitGroup |
| Protect a small piece of shared state | sync.Mutex or sync/atomic |
| Stream data between stages | Channels with range |
| Cancel a tree of goroutines | context.Context |
Pick the simplest option. A mutex is often clearer than a goroutine and three channels for a plain counter.
It means handling several channels in one goroutine, normally with a select loop, so you react to whichever channel is ready first.
Yes, in the sense that it blocks until a communication can proceed. Unbuffered channel operations also synchronize the sender and receiver at the moment of transfer.
Check the ok value from the receive and assign nil to the channel variable. A nil channel is never ready.
When several cases are ready it chooses pseudo-randomly, so each ready case has an equal chance over time, but it does not guarantee exact alternation.
select multiplexes channels: it waits for the first ready send or receive among many. Combine it with nil channels to retire inputs, with a closed done channel or context for shutdown, and with owner goroutines to protect state without locks.