Using Goroutines and Wait Groups for Concurrency in Golang

Arindam Roy
The Startup
Published in
4 min readSep 18, 2019

--

Photo by Safar Safarov on Unsplash

Golang is become increasingly popular due to its opinionated nature and intuitive approach to concurrent code.

One of the most basic use cases of concurrency is to be able to process a list of values where the values are not codependent and the order of execution is irrelevant. We will be looking at that use case below.

The Problem

Let us set up a scenario.

Suppose we have a list of strings which we need to process. Processing will involve sleeping for a random amount of time between 0 and 10 microseconds, and then printing out the current string.

When we run this function, the output looks like this:

Current value is : a 
Current value is : b
Current value is : c
Current value is : d
Current value is : e
Current value is : f
Current value is : g
Current value is : h
Program finished executing

This is of course, expected. Now let us see how to parallelise this and pass on the execution of each of these elements in the…

--

--

Arindam Roy
The Startup

Product @hyperverge. I write about a lot of things, mostly tech.