This is a creation in Article, where the information may have evolved or changed.
go provides sync packet and channel to resolve synchronization and communication. The novice is more likely to create deadlocks for channel channels, and if the buffered channel also takes into account the rate at which the channel is put in and out of the data. Literally can understand, sync. Waitgroup is waiting for a group of threads to end. It implements a similar task queue structure where you can add tasks to the queue, remove the task from the queue when the task is complete, and if the tasks in the queue are not all complete, the queue will trigger blocking to prevent the program from continuing to run. Sync. Waitgroup has only 3 methods, ADD (), Done (), Wait (). where done () is add (-1) is aliased. Simply put, using Add () adds a count, done () minus a count, the count is not 0, and the wait () is blocked from running. A simple example is as follows: PackageMainImport ( "FMT" "Sync") var waitgroup sync. Waitgroup func Test (Shownumint) {fmt. Println (Shownum) waitgroup. Done ()//Task completion, the number of tasks in the task queue-1, in fact. Done is. ADD ( -1)} func Main () { forI: = 0; I < 10; i++{Waitgroup. ADD (1)//each creation of a goroutine, the number of tasks in the task queue is +1go Test (i)} waitgroup. Wait ()//. Wait () This will block until all the tasks in the queue are terminated.Fmt. Println ("done!")} This article from: Open source China Blog Thanks to the author: Heishing Lords View original: Golang sync. Waitgroup Resolving Goroutine Synchronization