Apply buffered vs unbuffered channels, select statement, done channel cancellation, fan-out, and fan-in patterns for concurrent Go programs.
0 / 5 completed
1 / 5
What is the difference between a buffered and unbuffered Go channel?
Buffered vs unbuffered: unbuffered channels guarantee that send and receive happen simultaneously — a form of synchronisation. Buffered channels act as queues; the sender only blocks when the buffer is full. Use unbuffered for synchronisation signals, buffered for work queues where producers and consumers run at different rates.
2 / 5
How does the select statement work with multiple Go channels?
select statement:select { case msg := <-ch1: ...; case <-timeout: ...; default: ... }. Without default, select blocks until one channel is ready. With default, it returns immediately if no channel is ready — useful for non-blocking channel polls. Select is the idiom for combining a done channel with work channels in cancellable goroutines.
3 / 5
What is the done channel pattern in Go and what problem does it solve?
Done channel pattern:done := make(chan struct{}); close(done) broadcasts to all goroutines selecting on it. This is the foundation of Go's context.Context — ctx.Done() returns a channel that is closed when the context is cancelled. Goroutines select on ctx.Done() to exit cleanly on cancellation or deadline.
4 / 5
What is the fan-out pattern in Go concurrency?
Fan-out: multiple goroutines read from a single work channel: for i := 0; i < workers; i++ { go worker(jobs, results) }. Each worker reads jobs from the shared jobs channel — Go's channel receive is safe for concurrent goroutines. Fan-out parallelises CPU-bound or I/O-bound work across a pool of goroutines.
5 / 5
What is the fan-in pattern in Go and how is it implemented?
Fan-in:func merge(cs ...<-chan int) <-chan int { out := make(chan int); for _, c := range cs { go func(c) { for v := range c { out <- v } }(c) }; return out }. Fan-in is the counterpart to fan-out — aggregate results from multiple parallel workers into one result channel. Use a sync.WaitGroup to close the output channel after all sources are drained.