
Test Goroutines and Channels in Go Code Without Sleeps
Test Go goroutines and channels without time.Sleep: range until the channel closes, use a select guard timeout, sort unordered results and check drains.
Tag
13 articles

Test Go goroutines and channels without time.Sleep: range until the channel closes, use a select guard timeout, sort unordered results and check drains.

Three small channel helpers in Go: or-done to wrap a range loop with cancellation, tee to duplicate a stream, and or to combine several done signals.

Channels or mutexes in Go, decided with a benchmark and a rule of thumb: who owns the data, whether a reply is needed, and what each choice costs.

Compare range-over-func iterators and channel generators in Go 1.23 and later: cost per item, early break, cancellation, iter.Pull and when channels win.

Merge many channels into one in Go with a generic function: who closes the output, stopping early without leaks, reflect.Select for dynamic sets.

Build a typed in-process pub sub broker in Go from scratch: topics, per-subscriber buffers, a drop policy for slow consumers, safe unsubscribe and tests.

How a Go channel is built: the hchan struct, the ring buffer, wait queues, direct hand-off to a waiting receiver, and what the compiler emits for select.

Who may close a Go channel and when: the single-sender rule, closing after multiple senders with a WaitGroup, close as a broadcast, and the panics to avoid.

A full guide to Go channels: send and receive, blocking, buffered and unbuffered behavior, close and range, directions, nil channels, ownership and costs.

Fix panic send on closed channel in Go: reproduce it, find the racing close, and apply ownership fixes for one or many senders, with context to stop them.

A nil channel blocks forever, so its select case is dead. Use that to merge streams, switch cases off, and enable a send only when a batch is ready.

The done channel pattern in Go: how a closed channel broadcasts a stop signal, how it relates to context, the any-done helper, and when to use a context.