Go, Software Development

Golang Concurrency With Goroutine: Things To Know

How far along are you with your learning of Golang? You will inevitably encounter Golang concurrency with goroutine. If case you are already there and struggling, this post will make understanding goroutines painless.


Goroutines Enable Concurrency

The concept of concurrency is different from Parallel programming, and Golang can enable both or either via goroutines. To keep this post simple, let us stick with concurrency. Goroutines allow our codes to perform multiple tasks virtually simultaneously (but not entirely) using one CPU processor or a limited number of processors. With concurrency, each task takes turns in using the processor to complete its task. Meaning, at any one point, the processor only processes a single task. However, the processing happens so quickly that it makes us believe the processor works on multiple tasks simultaneously.

The Main Function is the main goroutine

The Golang main function that we are well familiar with is a goroutine. Moreover, it is the main goroutine in an application. Does it mean other functions are also goroutines? Although Golang can turn a function into a goroutine for concurrency, not precisely. Normal functions run sequentially. For instance, when we call two functions, the first function completes its operation before the second function can start running.

Therefore, there is no concurrency in Golang codes without goroutines. How do we turn an ordinary function into a goroutine, then? The answer is simple, but it will not help us create a proper goroutine. So, the answer is to use the keyword go before a function call. For example, consider the following codes.

Goroutines Are Usually Functions

There is a way to group codes, and we can do that in Golang using functions. Each function could represent a unit of work that is repeatable and reusable. Therefore, functions (including anonymous functions) are perfect candidates for goroutines.

Using The go Keyword Is Not Enough

One problem is naively using the go keyword to turn a normal Golang function into a goroutine to achieve concurrency. Doing so, as a result, causes both the main function and the naive goroutine to run concurrently. However, the main function usually completes first before the goroutine even starts. For example, consider the following codes that we modified a bit.

When we run the codes, we consistently get the following output.

How do we fix it?

Control Golang goroutine Concurrency With Channels

Use channels to control goroutines in Golang and synch up the concurrency. Channels allow the main function to communicate with the goroutines and vice versa. For example, consider the following codes that we modified to use a channel.

When we run the codes, we get the following results.

We tested the codes using version go1.17.8 for windows/amd64.


Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.

You Might Also Like