Golang Tutorial | How To Implement Concurrency With Goroutines and Channels
For folks who want to have a thorough walk through can watch the video linked below:-
Golang is a very efficient language. Much of this efficiency can be due to its unique concurrency abstractions. For example, Java threads are mapped to OS threads, whereas Go utilizes its own goroutines scheduler to further insulate its lightweight goroutines from OS threads. In summary, Golang uses OS threads sparingly; if a goroutine becomes stuck, Go’s scheduler will replace it with another goroutine to keep the thread occupied as much as possible. Because each CPU core can only handle a certain number of threads (and generating new threads is expensive), keeping these threads busy is essential.
So, how can we create several http calls in Golang at the same time? You may have used async/await to make several API requests if you’ve used C# or current JavaScript. It’s not quite as simple with Golang, but it’s all in the name of efficiency. At least one goroutine is constantly running in Go, which is responsible for running the main program (). With the term go before the function call, we can spawn new routines. Goroutines may remind you of the concept of context if you’ve worked with Java/C# asynchronous calls. The developer can create thousands of these lightweight goroutines with Go Scheduler, and it will manage the CPU time spent on each one for us.The main goroutine continues on its path immediately after spawning a new goroutine, until it reaches a blocking operator (akin to an await in C# or Js).
Sequential Async Call
Let’s start with a basic console program that connects to a few website urls and tests if the connection is successful. There are no goroutines at start, and all calls are made in order which is not very efficient.
Adding Concurrency and Optimizing the code
Goroutines and Channels are a built-in lightweight functionality for managing concurrency and communication between multiple concurrently running functions. This allows you to write code that runs independently of the main program and returns a value or values (or nothing if it’s merely an independent action). For this, Go has two keywords: go and chan.
First we need to add a channel. Since golang functions running in their own goroutines are just simple functions, we need a way for the inner goroutine to pass its result to the outer goroutine; this is done through channels. We simply initialize them with: c := make(chan string) We can use the <- arrow to send the resulting value to our channel, which we can also use to assign values from the channel.
Second, we need to add a tracker of sorts, to keep track of how many values we should be expecting to come out of this channel. This can be done using the type sync.WaitGroup.
Implementing these two ideas, results in the following:
Result
We can see for the second program, our app took very less time. Thanks to Goroutines.
Conclusion
Today we looked at How To Implement Concurrency With Goroutines and Channels.
Follow me for updates like this.
Connect with me on:-
Twitter 👦🏻:- https://twitter.com/kmmtmm92
Youtube 📹:- https://www.youtube.com/channel/UCV-_hzlbVSlobkekurpLOZw/about
Github 💭:- https://github.com/Kavit900
Instagram 📸:- https://www.instagram.com/code_with_kavit/