Concurrency in Golang

Photo by Chinmay B on Unsplash

Concurrency in Golang

Concurrency in Golang is the ability for functions to run independently of each other.

Sequential vs Concurrency

First, Let's's try to understand sequential and concurrency processing.

Sequential Processing

Sequential Programming is one of the simplest forms of programming where tasks are linearly executed one after another.

This means the next command will not be executed until the previous command is finished executing.

It is often used where programs or commands are not dependent on each other.

Let's see an example of sequential processing in golang then we will run the same function using go-routines there you will able to see the difference.


func calculateSquare(i int){
    time.Sleep(1 * time.Second)
    fmt.Println(i * i)
}
func main() {
    start := time.Now()
    for i := 1; i <= 1000; i++ {
        calculateSquare(i)
    }
    elapsed :=  time.Since(start)
    fmt.Println("Function took", elapsed)
}

We have added the time here to calculate the time taken by the entire program to run.

Concurrency Processing

Concurrency processing is the ability of a system to perform multitasking. Where it can execute more than one task simultaneously. This happens in the CPU by switching between jobs.

Concurrency is the notion of multiple things happening at the same time. It is potential for multiple processes to be in process at the same time.

Concurrency in multiple-core CPUs will look like this.

Now Let's see how go help us to achieve concurrent programming. In go we have something called Go-routines. Let's see what is go-routines.

Go-routines

  • Go Routines can be considered as a lightweight thread that has separate independent execution.

  • It can also execute concurrently with other goroutines.

  • Go-routines are managed by go-run time schedulers.

Let's see the syntax how go-routines are created.

go calcuate()

In the above snippet you can see. Go uses special keyword go . whenever we place this keyword before a function or a method call. That function or method will now be executed as a go-routines. Here we have a function calculate which is going to run in a separate goroutine.

Let's see an example by running the above code snippets which we used for sequential processing. Here we will use the same code with go-routines.

func calculateSquare(i int){
    time.Sleep(1 * time.Second)
    fmt.Println(i * i)
}
func main() {
    start := time.Now()
    for i := 1; i <= 1000; i++ {
        go calculateSquare(i)
    }
    elapsed :=  time.Since(start)
    time.Sleep(2 * time.Second)
    fmt.Println("Function took", elapsed)
}

Here, In the above code you might able to see the difference we have introduced two changes. One is for sure we have added go keyword to make our calculateSquare() function go-routines. And the second line of code is time.Sleep(2 * time.Second) why we added this ? Well this is added here so that our main function wait for our go-routines to finish. And I am assuming this go-routines will take less then 2 seconds to finish. We can also replace sleep with waitGroup which is available in go .Later we will see example with waitGroup as well in some other post.

Did you find this article valuable?

Support aditya kumar by becoming a sponsor. Any amount is appreciated!