This is the 27th day of my participation in Gwen Challenge

These reviews

If you don’t already know Go, I suggest you read mineGo from zero.

In this series of articles, I’ll take a closer look at the Go language and introduce advanced features such as package management, reflection, and concurrency.

After understanding the basic threading model in Go, we will formally enter into the concurrent programming practice of Go, in which we will mainly introduce the use and characteristics of Goroutine and channel, and explain some concurrent tools in sync package.

Coroutines goroutine

The coroutine Goroutine is a lightweight thread in Go that is managed at run time by Runtine, and the main function we wrote in the previous code also runs on goroutine. Start a new Goroutine in the following format:

goExpression statementCopy the code

Whenever such a GO statement appears in the code, an expression is executed concurrently. Expression statements can be either built-in functions or custom methods and functions (named or anonymous). Let’s take a look at this example:

package main import ( "fmt" "time" ) func test() { fmt.Println("I am work in a single goroutine") } func main() { go Test () // Goroutine Sleep 1 s time.sleep (time.second)}Copy the code

The output is expected to be:

I am work in a single goroutine
Copy the code

We first declared a simple test function, and then let the test function execute concurrently on the Goroutine. After the go statement, we also rest the main goroutine for 1 second with the time#Sleep function. If we execute the go statement without letting the main Goroutine rest, it looks like this:

package main

import (
	"fmt"
	"time"
)

func test()  {
	fmt.Println("I am work in a single goroutine")
}

func main()  {
	go test()
}
Copy the code

When you run it, you will find that in most cases the print statement in the test function is not executed. In Go, as soon as the main goroutine(the main function) ends, it means that the entire program is finished running. If go test() is not scheduled by the scheduler before the end of main, the print statement cannot be executed. As with other programming languages, the order of code in different threads is not indicative of the true order of execution, and this is something we need to pay attention to in future code writing.

We can use a more complex example to illustrate:

package main
import (
	"fmt"
)
func setVTo1(v *int)  {

	*v = 1
}
func setVTo2(v *int)  {
	*v = 2
}

func main()  {
	v := new(int)
	go setVTo1(v)
	go setVTo2(v)
	fmt.Println(*v)
}
Copy the code

The result of this code can be multiple cases, and the final *v output can be 0, 1, 2, although most cases are 0. Depending on the scheduler schedule at the time, we can’t make too many assumptions about the order of concurrent execution, which can cause unexpected bugs.

The go statement starts anonymous or unnamed functions as follows:

Package main import (" FMT ""time") func main() {go func(name string) {FMT.Println("Hello" + name)}(" Xuan ") // Main Goroutine Sleep 1 s time.sleep (time.second)}Copy the code

In the code above, we started a concurrent thread of an anonymous function with the GO statement. To ensure the output of the result, we let the main Goroutine sleep for 1 s.

summary

This paper mainly introduces the application practice of coroutine Goroutine. The Go language supports concurrency, so all you need to do is open Goroutine with the Go keyword. Goroutine is a lightweight thread, and goroutine scheduling is managed by the Golang runtime. Through the explanation of this article, we understand the basic usage of Goroutine, the following can be more practice to deepen the understanding of Goroutine.

Read the latest article, pay attention to the public number: AOHO Qiusuo