Loading, please wait...

A to Z Full Forms and Acronyms

What is Concurrency in Go Language? | Go Programming Tutorial

Nov 20, 2022 #GoProgramming #GoLanguage, 740 Views
In this article, you will learn about: What are Goroutines? How to create a Goroutine? Advantages of Goroutines

What is Concurrency in Go Language? | Go Programming Tutorial

In this article, you will learn about:

  • What are Goroutines?
  • How to create a Goroutine?
  • Advantages of Goroutines

What are Goroutines?

Go Programming Language has a special feature known as Goroutines. A Goroutine is a function or method which executes independently and simultaneously in link with any other Goroutines present in your program. In simple words, every concurrently executing activity in GoLang is called a Goroutines. Goroutines are considered lightweight thread. The cost of creating Goroutines is extremely small compared to other threads. Every program contains at least one Goroutine and that Goroutine is known as the main Goroutine. All the Goroutines are working under the main goroutines if the main goroutine is terminated, then all the goroutines present in the program are also terminated. Goroutine works in the background.

How to create a Goroutine?

The user can create their Goroutines simply by using the go Keyword as a prefixing to the function or method call. The syntax of creating the Goroutine is:

Syntax:

func go_routine()

{

     // statements

}

// go keyword is used as the prefix of your function call

go go_routine

Advantages of Goroutines

  • The cost of the Goroutines is cheaper than threads. 
  • Goroutines are stored in the stack data structure and the size of the stack can expand and shrink according to the requirements of the program. However, the size of the stack in the threads is fixed.
  • Goroutines can communicate with the help of channels and these channels are mainly designed to prevent race conditions when accessing shared memory using Goroutines. 
  • The program has many threads and that thread has many Goroutines associated with it. If any of the goroutine functions block the thread due to resource requirements then all the remaining goroutines create the new OS thread. All these details are hidden from the programmers.
A to Z Full Forms and Acronyms