Analytics Vidhya
Published in

Analytics Vidhya

Golang channels usage in pipeline

Channels can be used in different ways including sending values to go routines or communication between go routines which is the core functionality.

One important use of channels is to control the flow of pipeline stages. Pipelines are used as stages with multiple down streams and up streams where data flows through channels.

There is a beautiful blog written on pipelines and their use inside golang.

In go routines the channels can be a great tool to control the flow in pipeline stages where each stage do some processing on the value send through channel and return the value on outbound channel which further can be processed as per the requirement.

For handling data coming on multiple channels in a go routine defined inside the pipeline stages, use select which provides a way to exit or to process data coming on channels.

Let’s go with an example what will be the approach when you are working with golang channels and how to use them as your strongest tool.

If we go with above example we can see that channels are used to control data flowing through go routines. One concept I have learned from channels after working for 4 years in golang is that channels can be used not just to signal other go routines but to control them (when to exit the processing).

When we talk about controlling go routines using the channels we need to pass the same channel to all the go routines which we can use as a control over those go routines. So if we close that shared channel and checking the status of channel using select it will send the value of closed status on all go routines which are sharing the same signal channel. This concept of sending a signal to all go routines using a single channel can be used in different scenarios accordingly.

In above example we use done channel which sends signals as the empty struct to notify all the go routines.

Example:

Suppose we want to insert a user data into database, if inserted we need to fetch the value on the api or do other stuff with the same value using multiple go routines working on the same value.

But what if the user is not inserted and there is an error , then send the value to all the go routines which are waiting the value on input channel to not wait as there is an error. Here comes the channel, you can notify other functions that something goes wrong. Use a select which will be waiting for either the value or a signal which will tell the go routines to exit in case of an error in parent go routine.

In the above example I have send a signal by closing the channel and used select to wait for the user channel or done channel.

Conclusion:

The channels can be used to control multiple go routines providing the condition that same channel is passed as a signal channel to all go routines if the go routines are linked with each other on the data.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Himanshu Chaudhary

Himanshu Chaudhary

Cloud Engineer, Kubernetes, Docker, CI/CD, Golang, gRPC, Micro services, git, Infrastructure as a Service, Storage, Compute, Protobuf, REST.