Skip to main content

Concurrency with Goroutines in Golang

In our previous tutorial, we have explained how to work with Maps in Golang. In this tutorial, we will explain about Goroutines in Golang. Goroutines are functions in Golang that are run concurrently which means multiple operations can be handled at the same time. It’s about creating and executing multiple processes independently.

With Goroutines, we can easily convert sequential programs into concurrent program without having to worry about thread pools as the Golang creates very light-weight threads that’s managed by Go runtime.

Also, read:

For example an eCommerce website require many concurrent tasks to execute like:

  • Display the time counter for upcoming Sale.
  • Display the latest deals and products banner.
  • Display currently online users.
  • Update product stock details.
  • Update the user cart details when product selected.

above tasks are needed by eCommerce website running at the same time to keep details updated to customers. So there needs to run multiple task at the background to fulfill the requirement. So for the above scenario, we can use Golang Goroutines to run multiple tasks concurrently.

We will cover following in this tutorial:

1. What is Goroutines in Golang

In Golang, Goroutines are ability for functions to run independently. When there are multiple operations needed to perform at the same time, the Goroutines can be used to handle this.

The functions Goroutines call makes functions to run immediately and running continues in background while rest of the program continues its execution.

2. How to create Goroutines in Golang

The keyword go used before function calls to run functions as Goroutines. There are multiple functions can be called as Goroutines to run concurrently.

// Normal function call that executes synchronously and waits for completing it.
displayMessage()

// Goroutines call that executes asynchronously and doesn't wait for completing it.
go displayMessage()

When function called normally, it executes synchronously and waits for completing it. While when Goroutines called, executes asynchronously and doesn’t wait for completing it.

3. Concurrency with Golang Goroutines

We can make Golang Goroutines by using go keyword as a prefixing to the function or method call.

package main 
  
import "fmt"
  
func displayMessage(message string) { 
    for i := 0; i < 5; i++ { 		
        fmt.Println(message) 
    } 
} 
  
func main() { 
  
   go displayMessage("Golang Goroutines call") 
  
   displayMessage("Noraml call") 
} 

in above example, we have created function displayMessage() and call the function normally and with Goroutines. When we will run this example code, it will display the result of normal function call. it does not display the result of Goroutines function call because when a new Goroutine executed, the Goroutine call return immediately. It does not wait for Goroutine to complete execution as it move to the next after the Goroutine call and skips the values returned by the Goroutine. So we need to make changes into this example code to work Goroutine call correctly.

Now, when we run above program, we should see the following output:

Noraml call
Noraml call
Noraml call
Noraml call
Noraml call

Now in below example code, we have added Sleep() method that makes our program sleeps for 1 second. In between 1 seconds, the new Goroutine function executed and terminates after 1 seconds main Goroutine to perform operations. The program execution continues till the main Goroutine terminates. In this example, both the Goroutine and the normal function call work concurrently.

package main 
  
import ( 
    "fmt"
    "time"
)
  
func displayMessage(message string) { 
    for i := 0; i < 5; i++ { 
	time.Sleep(1 * time.Second) 
        fmt.Println(message) 
    } 
} 
  
func main() { 
  
   go displayMessage("Golang Goroutines call") 
  
   displayMessage("Noraml call") 
} 

Now, when we run above program, we should see the following output:

Golang Goroutines call
Noraml call
Golang Goroutines call
Noraml call
Golang Goroutines call
Noraml call
Golang Goroutines call
Noraml call
Golang Goroutines call
Noraml call

You may also like: