Instagram
youtube
Facebook
  • 1 year, 7 months ago
  • 737 Views

Concurrency tutorial golang and goroutines

Yaman Birla
Table of Contents

Hello Tech enthusiast, Today we are going to learn about Concurrency. 1. How is concurrency implemented in Golang? 2. Is Golang parallel or concurrent? 3. How do you get concurrency in Go? 4. What is multithreading Golang? 5. And how Golang has rich support for it using Goroutines and channels but in this blog, we are going to learn about Goroutines.

In the field of programming or Software development, every problem is divided into sub-smaller ones or we can say there are many ways to approach a problem like top to down, bottom - up. But it is considered ideal for making smaller components of a larger program and running them simultaneously especially in the case of web servers to handle multiple requests. So, just like that making progress on more than one task simultaneously is termed Concurrency. 

Golang is highly supportive of concurrency and provides special features known as Goroutines.

 

What is Goroutine? What is goroutine used for?

A Goroutine is a type of special function which has the capability of running independently or simultaneously with other functions. 

In order to create a goroutine simply use the go keyword followed by a function invocation:


package main

import "fmt"

func simple(m int) {
	for j := 0; j < 15; j++ {
		fmt.Println("here ", m, ":", j)
	}
}

func main() {
	go simple(0)
	fmt.Println("Completed invocation")
}

 


 

In the above code, we have implemented a simple function which takes an integer, and in the function the loop prints out the looping index to 14. we call this function using the go keyword i.e. goroutine. However, there is a problem with this program, and is that it only prints the “Completed invocation” line  

 

 

It is because when a goroutine is executed, the Goroutine call return immediately as the control does not wait for the goroutine to complete its execution and moves to the next line and ignores the value returned by the Goroutine. 

 

How is concurrency implemented in Golang?

In order to execute this program in a systematic manner, the following changes are must:


package main

import (
	"fmt"
	"time"
)

func simple(m int) {
	for j := 0; j < 15; j++ {
		time.Sleep(1 * time.Second)
		fmt.Println("here ", m, ":", j)
	}
}
func main() {
	go simple(0)

	simple(0)
	fmt.Println("Completed invocation")
}

 


Output:

 

We added the Sleep() method in our program which makes the main Goroutine sleeps for 1 second in between 1-second the new Goroutine executes, prints its looping variable, and then terminates after 1-second main goroutine re-schedule and displays its looping variable.

So, as you can see here, both Goroutine and normal function works concurrently.

 

Advantages of using Goroutines

  • Goroutines are less expensive than threads.
  • Goroutines are stored on the stack, and the size of the stack can expand and shrink as needed by the program. However, in threads, the stack size is fixed.
  • Goroutines can interact over the channel, and these channels are specifically designed to prevent race problems when Goroutines access shared memory.

Add a comment: