This is a created article in which the information may have evolved or changed.
When learning the go language, the WAITGOURP structure in the "Sync" package is used to achieve the completion of the control task.
Since Waitgroup is a structure, all needs to pass through sync. waitgroup{} to create
Very similar to the Countdownlatch class in the latch countdownlatch when the Shoper/java concurrent library is inverted.
ADD (), Done (), Wait (). where done () is the alias of Add (-1). Simply put, using Add () adds a count, done () minus a count, the count is not 0, and the wait () is blocked from running.
Package Mainimport ("FMT" "Runtime" "Sync") func main () {runtime. Gomaxprocs (runtime. NUMCPU ()) Waitgourp: = Sync. Waitgroup{}waitgourp.add (Ten) for I: = 0; I < 10; i++ {Go Go (&waitgourp, i)}waitgourp.wait ()}func Go (WG *sync. Waitgroup, index int) {sum: = 0for I: = 0; i < 100000000; i++ {sum + = i}fmt. PRINTLN (index, SUM) WG. Done ()}
PS: Because the previous use of Java, the method's parameter pass is the value of the pass, and in this go is the difference between the value of the pass and the reference passed.
Because the structure is a value type, the reference (address) of the struct should be passed as a method parameter.