Learn Go: Introduction to Concurrency in Golang #27
Golang is a language well known for its amazing concurrency, lets learn concurrency in Go
Golang (Go) is a programming language known for its concurrency and efficiency. And it is the fastest-growing programming language is backed by Google. Go provides rich support for concurrency via goroutines. This blog is will introduce the approach of concurrency in Golang.
Goroutines are lightweight threads that execute more than one task simultaneously. here we will take a look at how we can use Goroutines. Before getting into Goroutines we need to understand what is concurrency and how it differs from parallelism.
It would be ideal for programs like these to be able to run their smaller components at the same time (in the case of the web server to handle multiple requests). Making progress on more than one task simultaneously is known as concurrency. Go has rich support for concurrency using goroutines and channels.
Go is a concurrent language and not a parallel one. Before discussing how concurrency is taken care in Go, we must first understand what is concurrency and how it is different from parallelism.
What are Concurrency and Parallelism?
In programming, concurrency is the composition of independently executing processes, while parallelism is the simultaneous execution of (possibly related) computations.
Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once. A concurrent program has multiple logical threads. These threads may or may not run in parallel
Concurrency and parallelism comes into the picture when you are examining for multitasking and they are often used interchangeably, concurrent and parallel refer to related but different things.
Concurrency - Concurrency is about to handle numerous tasks at once. This means that you are working to manage numerous tasks done at once in a given period of time. However, you will only be doing a single task at a time. This tends to happen in programs where one task is waiting and the program determines to drive another task in the idle time. It is an aspect of the problem domain — where your program needs to handle numerous simultaneous events.
Parallelism - Parallelism is about doing lots of tasks at once. This means that even if we have two tasks, they are continuously working without any breaks in between them. It is an aspect of the solution domain — where you want to make your program faster by processing different portions of the problem in parallel.
A concurrent program has multiple logical threads of control. These threads may or may not run in parallel. A parallel program potentially runs more quickly than a sequential program by executing different parts of the computation simultaneously (in parallel). It may or may not have more than one logical thread of control.
Popular programming languages such as Java and Python implement concurrency by using threads.
Go takes a different route. Following a model proposed by the renowned computer scientist Tony Hoare, Go uses the concurrency model called Communicating Sequential Processes (CSP).
Two crucial concepts make Go’s concurrency model work:
Goroutines —A goroutine is a function that runs independently of the function that started it. Sometimes Go developers explain a goroutine as a function that runs as if it were on its own thread.
Channels —A channel is a pipeline for sending and receiving data. Think of it as a socket that runs inside your program. Channels provide a way for one goroutine to send structured data to another.
We won’t spend time on the theory or underpinnings of the goroutine and channel systems, but will stick to practical use of these two concepts.
In the next tutorial, we will be looking at go-routines & channels.