Go offers subprograms to run their actions simultaneously with the help of Concurrency. Various programming languages implement concurrency but Go does this with a twist. It doesn't follow the threading model for concurrency; it follows the Communicating Sequential Processes(CSP) model. Go supports concurrency with goroutines and channels. In this article, you will go through various aspects of Go Concurrency.
What Is Concurrency?
Concurrency is a process that deals with lots of activities operating simultaneously. Let's understand the concept with the help of an example.
Let's consider a person driving a car. While driving, he feels hungry and reaches a drive-thru and collects a food parcel. He halts his car into a parking lot, eats the food, and then starts driving again. Here, the person is capable of performing both actions simultaneously. But he performs actions in a sequence. This is concurrency.
How Is Parallelism Different From Concurrency?
Both produce the same result but with different approaches. Parallelism is the process to perform various actions simultaneously, particularly not in sequential order. Let's understand Parallelism with the same example.
Let's assume the person is driving the car and simultaneously eating the food. In this scenario, the person performs both actions simultaneously(parallel). This is Parallelism.
Parallelism doesn't always result in faster execution time. In Parallelism, actions are performed parallely in multiple cores, creating the need for components to communicate with each other. Concurrent systems have a low communication overhead.
Concurrency in Go
Popular programming languages like Python and Java use thread pools to execute concurrency whereas Go has built-in features, Goroutines and Channels to achieve Concurrency. Goroutines are much easier to manage than thread pools that are purely operated by the operating system. Go works on the Communicating Sequential Processes (CSP) concurrency model.
A goroutine is a function that executes concurrently with other functions or goroutines. Goroutines work in the background while the main part of your program goes on to do something else. Goroutines are not OS threads. To work with Goroutine, you have to prefix the go keyword to the function or method. A goroutine is a lightweight thread managed by the Go runtime.
- Every program consists of at least one Goroutine and that Goroutine is known as the main Goroutine.
- Goroutines are deeply integrated with Go’s runtime. Go’s runtime monitors goroutine behavior, suspends them when they become blocked, and then presumes as they are unblocked.
- Goroutines are inexpensive compared to threads. They possess only a few kb in stack size and the stack can expand and contract according to the application whereas the stack size is fixed in the case of threads.
Let’s see an example:
We created a number() function and defined it in two ways: first one is a Goroutine, i.e. go number(“11”) and another one is a normal function, i.e. number (“77”).
We used the Sleep() method in our program that helps the main Goroutine sleep for 1 second. And in between 1-second, the new Goroutine executes, displays “11” as output, and then terminates. After 1-second, the main Goroutine re-schedules and performs its operation.
Goroutines communicate with other elements through channels.
Channels can be thought of as a passage through which Goroutines communicate. The channel is defined using the make function and the chan keyword. Each channel has a specified type defined with it. This type of data is only allowed to be transported by the channel.
The zero value of a channel is nil and they are not of any use until defined. Channels are designed to prevent race conditions when accessing shared memory using Goroutines
If you're eager to gain the skills required to work in a challenging, rewarding, and dynamic IT role - we've got your back! Discover the endless opportunities through this innovative Post Graduate Program in Full Stack Web Development course designed by our partners at Caltech CTME. Enroll today!
We have seen in the article how Golang holds this amazing concept of Goroutines, that implements concurrency in its own way. This has been one of the reasons for Go's popularity. We have learnt how Goroutines work and how they communicate through channels.
If you aim to build a software development career, you can check the Post Graduate Program in Full Stack Development by Simplilearn. It can be the ideal solution to help you build your career in the right direction.
If you have queries or would like to provide your inputs to our editorial team regarding this "The Supreme Guide to Go Concurrency" tutorial, feel free to use the comments section below. Our team of SMEs will get back to you soon!