Merge Channels in Go(lang)

Dr. Sandeep SadanandanJuly 8, 2019

Recently, I had to deal with receiving several events from Kafka. There were about 6 different types of events, but internally, the ways I had to deal with them were quite similar — so I decided for a generic method.

From Kafka, I was receiving events in individual channels (one channel per event type)

As my handling of the events was the one generic method, I wanted to take all the channels, merge them into a single channel which has all the events from all channels, combined*, from which I can pick one at at time**, and deal with it.

Doing it in Go was a breeze. Seriously, in 10 lines, all was done.

Here’s how I did it. (well, business events are replaced by integers here ;) )

I have two channels — one which delivers me odd numbers, and another one which delivers even numbers. And here’s how one can merge them.

Note that the function accepts a slice of channels — so, it doesn’t matter whether it’s just two channels, or 100.

For completion, here’s my sample program which contains the channel creations as well.

Run it as follows:

go run channelmerge.goAnd you’ll see all whole numbers being printed on your screen. (warning: infinite loop)

If you look closely, you’ll see that they are not all in order — of course, the channels are not synchronised with each other.

Cool. ah?

Signing off,
Sands.

*Combining the channels would give me a better solution than having 6 different loops to do the same thing, and having to add more such loops when I have more events-types**Naturally, at some point of time, I would want to speed that up,.. That we can do in another post for ThreadPooling

About the author

Dr. Sandeep Sadanandan

Dr. Sandeep Sadanandan

With two decades of experience, Sandeep brings onboard both theoretical and practical knowledge from a wide range of projects. Your ideas will blossom into wonderful products in his hands.

We have other interesting reads

Setting up Gitlab Runner on Amazon EKS: A Step-by-Step Guide

In the ever-evolving landscape of software development, Continuous Integration and Continuous Delivery (CI/CD) have become indispensable practices.

Tom JoseFebruary 27, 2024

From Proof-of-Concept to Production: Evolving Your Self-Healing Infrastructure

In the previous article, we explored building a self-healing nginx infrastructure using KAgent and KHook, covering autonomous configuration validation, intelligent analysis, and automated remediation.

Maryam NaveedDecember 4, 2025

MCP for Enterprise: Why Context-Driven AI Matters for Your Organization

Many organisations that tried AI in workplace have had it hard. Why? Lack of enterprise integration.

Dr. Sandeep SadanandanAugust 25, 2025