Multiplexing channels

Multiplexing describes the methodology where we use a single resource to act upon multiple signals or actions. This method is used extensively in telecommunications and computer networks. We might find ourselves in a situation where we have multiple types of tasks that we want to execute. However, they can only be executed in mutual exclusion, or they need to work on a shared resource. For this, we make use of a pattern in Go known as channels multiplexing. Before we dive into how to actually multiplex channels, let's try to implement it on our own.

Imagine that we have a set of channels and we want to act on them as soon as data is sent over a channel. Here's a naïve approach on how we want to do this:

// naiveMultiplexing.go ...

Get Distributed Computing with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.