Concurrency in Golang: How to use Goroutines? detailed guide

Go language provides special functions called Goroutines. A Goroutine is a function or method that executes independently and concurrently with any other Goroutines present in your program. In other words, activities executed concurrently in each Go language are called Goroutines. You can think of Goroutines as lightweight threads. The cost of creating Goroutines is very small compared to threads. Each program contains at least one Goroutine, and this Goroutine is called the main Goroutine. If the main Goroutine terminates, all Goroutines running under the main Goroutine, then all the Goroutines present in the program will also terminate. Goroutines are always running in the background.

 

How to create Goroutine?

You can create your own Goroutines simply by prefixing function or method calls with the go keyword, as shown in the following syntax:

The syntax is as follows:

func name(){
// statements
}

// using go keyword as the 
// prefix of your function call
go name()

example:

// Go program to illustrate
// the concept of Goroutine
package main
  
import "fmt"
  
func display(str string) {
     for w := 0; w < 6; w++ {
         fmt.Println(str)
     }
}
  
func main() {
  
     // Calling Goroutine
     go display( "Welcome" )
  
     // Calling normal function
     display( "lsbin" )
}

The output is as follows:

lsbin
lsbin
lsbin
lsbin
lsbin
lsbin

In the above example, we simply create a display() function and then call this function in two different ways, the first is a Goroutine that goes to show("Welcome") and the other is a normal function that shows( "lsbin"). But there is a problem, it only shows the results of normal functions, not the results of Goroutines, because when a new Goroutine is executed, the Goroutine call returns immediately. Instead of waiting for the Goroutine to finish executing like normal functions, they always advance to the next line after the Goroutine call and ignore the value returned by the Goroutine. So, to execute Goroutines correctly, we make some changes to the program, as shown in the following code:

Modified example:

// Go program to illustrate the concept of Goroutine
package main
  
import (
     "fmt"
     "time"
)
  
func display(str string) {
     for w := 0; w < 6; w++ {
         time .Sleep(1 * time .Second)
         fmt.Println(str)
     }
}
  
func main() {
  
     // Calling Goroutine
     go display( "Welcome" )
  
     // Calling normal function
     display( "lsbin" )
}

The output is as follows:

Welcome
lsbin
lsbin
Welcome
Welcome
lsbin
lsbin
Welcome
Welcome
lsbin
lsbin

We added the Sleep() method in our program, which makes the main Goroutine sleep for 1 second between the execution of the new Goroutine, and display "Welcome", then at 1 second the main Goroutine reschedules and Terminates after performing its operations. This process continues until after z < 6, the main Goroutine terminates. Here, Goroutines and normal functions work simultaneously.

Advantages of Goroutines

  • Goroutines are cheaper than threads.
  • Goroutines are stored in a stack, and the size of the stack can grow and shrink according to the requirements of the program. But in a thread, the size of the stack is fixed.
  • Goroutines can communicate using channels, and these channels are specially designed to prevent race conditions when using Goroutines to access shared memory.
  • Suppose a program has one thread, and that thread has many Goroutines associated with it. If any Goroutine blocks a thread due to resource requirements, all remaining Goroutines are assigned to newly created OS threads. All these details are hidden from the programmer.

 

Anonymous Goroutines

In Go language, you can also start Goroutines for anonymous functions, in other words, you can create anonymous Goroutines simply by prefixing the function with the go keyword, as shown in the following syntax:

The syntax is as follows:

// Anonymous function call
go func (parameter_list){
// statement
}(arguments)

example:

// Go program to illustrate how
// to create an anonymous Goroutine
package main
  
import (
     "fmt"
     "time"
)
  
// Main function
func main() {
  
     fmt.Println( "Welcome!! to Main function" )
  
     // Creating Anonymous Goroutine
     go func() {
  
         fmt.Println( "Welcome!! to lsbin" )
     }()
  
     time .Sleep(1 * time .Second)
     fmt.Println( "GoodBye!! to Main function" )
}

The output is as follows:

Welcome!! to Main function
Welcome!! to lsbin
GoodBye!! to Main function

For more information about Golang development, please refer to: lsbin - IT development technology : https://www.lsbin.com/

Check out more JS-related content below:

Guess you like

Origin blog.csdn.net/u014240783/article/details/115387219