Home  >  Article  >  Backend Development  >  What is golang asynchronous

What is golang asynchronous

青灯夜游
青灯夜游Original
2022-12-28 10:54:454435browse

In golang, asynchronous refers to execution not in accordance with the order of the code. The execution of an asynchronous process will no longer have a sequential relationship with the original sequence; in asynchronous, when an asynchronous process call is issued, the caller You can continue to perform subsequent operations before getting results. Golang asynchronous is mainly implemented by coroutine (goroutine); goroutine is used to call functions. When a function is called by the go keyword, go will create a coroutine-like thing to execute the function, so that it can be easily Achieve concurrency.

What is golang asynchronous

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

What is asynchronous?

Asynchronous (async) is a concept opposite to synchronous (Synchronous, sync). In the traditional single-threaded programming we studied, the running of the program is synchronous (synchronous does not mean that all steps run at the same time, but means that the steps are executed sequentially in a control flow sequence). The concept of asynchronous is a concept that does not guarantee synchronization. That is to say, the execution of an asynchronous process will no longer have a sequential relationship with the original sequence.

The simple understanding is: synchronization is executed in the order of your code, asynchronous execution is not in the order of the code, and asynchronous execution is more efficient.

Another explanation is:

Give you two containers, one is called synchronous and the other is called asynchronous, and the code to be executed is divided into synchronous and asynchronous. , execute synchronization first and wait for asynchronous compilation at the same time, and then execute the asynchronous code after the synchronization is completed.

How to implement asynchronous in golang

Go's asynchronous is mainly implemented by coroutine (goroutine). Compared with threads, coroutines occupy less memory and are more lightweight, which is also Go's performance advantage.

Go language is a concurrent programming language. There is a very important keyword go (goroutine) in Go. Generally, we can use it to do some asynchronous and concurrent tasks.
Goroutine is used to call functions. When a function is called by the go keyword, go will create a coroutine-like thing to execute the function, so that concurrency can be easily achieved.

Example:

package main
import (
	"fmt"
)
func main() {
	go work1()
	go work2()
	fmt.Println("[全部完成]")
}
func work1(){
	fmt.Println("work1")
}
func work2(){
	fmt.Println("work2")
}

Result:

[全部完成]

Of course this is an inappropriate example, but it also proves asynchronous execution, not waiting for output after calling work1, work2 As a result, execution continued.
If you want to wait for the asynchronous execution to complete before executing the final output, you need to join the WaitGroup

Correct example:

package main

import (
	"fmt"
	"sync"
)

var waitGroup sync.WaitGroup
func main() {
	waitGroup.Add(2)
	go work1()
	go work2()
	waitGroup.Wait()
	fmt.Println("[全部完成]")
}
func work1(){
	fmt.Println("work1")
	waitGroup.Done()
}
func work2(){
	fmt.Println("work2")
	waitGroup.Done()
}

Result:

work2
work1
[全部完成]

[Related recommendations:Go video tutorialprogramming teaching

The above is the detailed content of What is golang asynchronous. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn