This is a created article in which the information may have evolved or changed.
Package Mainimport ("FMT" "Sync" "Time") var waitgroup sync. Waitgroupfunc Test (x int) {FMT. PRINTLN (x) waitgroup. Done ()//task completed, number of tasks in the task queue-1, in fact. Done is. ADD ( -1)}func main () {FMT. Println (time. Now ()) for I: = 0; I < 10; i++ {Waitgroup. ADD (1) Go Test (i)}waitgroup. Wait ()}
Synchronization in the Golang is via sync. Waitgroup to achieve. Waitgroup: It implements a queue-like structure that can continue to add tasks to the queue, remove them from the queue when the task is complete, and, if the tasks in the queue are not fully completed, block by using the wait () function to prevent the program from continuing until all queue tasks are complete. The Waitgroup feature is that wait () can be used to block until all tasks in the queue are complete without having to sleep for a fixed time. However, the disadvantage is that you cannot specify a fixed number of goroutine. However, the disadvantage is that you cannot specify a fixed number of goroutine. This issue may be resolved by using the channel.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.