What is a Goroutine ? Find the right way to implement goroutine 🔥
Tags: go, goroutine, webdev, concurrency
Published: 2022-06-21 18:22:49

## Why Go ?
Go has been widely used in most of the popular production systems such as Kubernetes, Docker, Influx DB, etc. There are mainly 4 reasons behind adoption of Go at such a massive scale. 

- It's simple and elegant
- Provides built-in concurrency at scale
- Designed to run on multiple cores 
- It's Super fast performance 🚀

Goroutine is a way to achieve concurrency in Go 🔥. 
## What is a goroutine ?
A goroutine is a lightweight thread managed by the Go runtime. Unlike Operating System thread which consumes memory usually in the range of **MBs to function properly** these goroutines consume memory only in the **range of KBs**. So it's **easy and fast for go runtime to create goroutines than threads for concurrency**. Goroutines are not actual threads, these are multiplexed to actual OS threads or even you could assume that these goroutines are a kind of abstract layer over OS threads.

Due to it's very **small memory footprint**, it's easy for go runtime to create **thousands of goroutines in a fraction of milliseconds**. But creating thousands of OS threads is heavily dependent on **memory and the CPU**, so it's obviously slow compared with goroutines. 

As well as Goroutine does not have a Local Storage as a result goroutines are cheaper to implement than OS threads, so **goroutines are much faster in terms of boot time**.

Context switching is also faster in case of `go` than OS thread context switching, because OS threads are heavy resources.

On top of this **Go even has another coolest feature i.e. Channels**🔥. Channels are the built-in way to communicate with goroutines. In the case of OS threads, this communication is the most difficult thing you can think of.

So this how go achieved better concurrency than traditional programming languages, which made **go a default language of choice for most of the use cases**.   

## Syntax for goroutine
The most difficult part of goroutine is to understand it more than its real implementation. So take your time to understand it before diving deep into the actual implementation.

In other programming languages you might have used the `async await` pattern for concurrency(Node.js and python - async await). But here it's even simpler than that. Just add the `go` keyword to your function prefix( e.g : `go someFunctionName`), now go runtime executes the same function in concurrency mode. See how cool is this 😆.

```
	go addTwoNumbers(1, 2)
``` 
## Implement a program without goroutine
Below is the snippet for adding 2 numbers. The `addition` function has a `sleep` command to mimic the processing time.

{% embed https://gist.github.com/rahul-yr/eaf3e7ac78f72849b3d234affabfb573 %}

If you execute the program using the `go run sequential.go` command, you will get the output as below(result as summation of 2 numbers).

![sequential-out.PNG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkf6dqzfq8wg9s70t76d.PNG)

## Implement a program with goroutine
Now let's convert this sequential execution to concurrency by adding `go` to the prefix of the caller. Below is the snippet.

{% embed https://gist.github.com/rahul-yr/9788dc3598eda9c4521d255f99549c50 %}

If you execute now using this `go run concurrency_without_wg.go` command, you will immediately notice that the output of **addition function is not displayed**.  

![concurrency_without_waitgroups_out.PNG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/og58tlmml4oufz2zz29p.PNG)

If you reached this far, then you have **successfully executed your first program in concurrency mode** 🔥. 

> But why is there a discrepancy in the output ?

This is happening because go runtime is not waiting for goroutines to finish their execution. So you need to explicitly update the details about goroutines to go runtime. This is where `wait groups` comes into picture. That's the exact problem wait groups solve.

### What are Wait groups ?
WaitGroup(`sync.WaitGroup`) is a mechanism to wait till the collection of goroutines finishes their tasks.
There are mainly 3 helper functions available in golang to implement this feature. 
- Add()
- Wait()
- Done()

`Add(5)` method takes an integer as an argument. This integer represents the number of goroutines available in this collection to the go runtime. `Wait()` tells the go runtime to wait exactly at this position till the collection of goroutines finishes their execution. `Done()` method is used to inform go runtime that this particular goroutine finished its execution.

Below is the code snippet with 1 goroutine. Refer to the comments for deeper understanding.

{% embed https://gist.github.com/rahul-yr/c7015fdfdcf7d5f5830a8e2acd0a6c50 %}

If you execute now, you will get the desired output as below.

![concurrency-out.PNG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq148phvx3tpvzla99tx.PNG)

## Summary
You have successfully implemented goroutines 🔥, but without channels (A way to communicate with goroutines) this topic is never gonna be completed. 

To stay updated on `Channels` you can follow this blog.

I would 💝 to hear your feedback and comments on the great things you're gonna build with this. If you are struck somewhere feel free to comment. I am always available.

If you feel this article is helpful for others then please consider a like.

You can find the complete code at [github](https://github.com/rahul-yr/learn-go-concepts.git)