Home >Backend Development >Golang >Why are WaitGroup\'s Add, Done, and Wait functions called using a pointer even when declared as a variable?
Pointer or Variable in WaitGroups Reference
Within the sync package, the Add, Done, and Wait functions for WaitGroups are all called by a pointer to a WaitGroup. However, the following code appears to contradict this convention:
package main import ( "fmt" "sync" "time" ) func worker(id int, wg *sync.WaitGroup) { defer wg.Done() fmt.Printf("Worker %d starting\n", id) time.Sleep(time.Second) fmt.Printf("Worker %d done\n", id) } func main() { var wg sync.WaitGroup for i := 1; i <= 5; i++ { wg.Add(1) go worker(i, &wg) } wg.Wait() }
In this code, while Done is called using a pointer variable, Add and Wait are called using a variable (not a pointer).
Explanation:
Despite the variable declaration var wg sync.WaitGroup, the Add, Done, and Wait functions are accessed through their pointer receiver (*WaitGroup). The value of wg is implicitly converted to a pointer by the Go compiler. This is necessary because the methods are defined as pointer receivers, as seen in the function declarations:
Add -------> func (wg *WaitGroup) Add(delta int) Done ------> func (wg *WaitGroup) Done() Wait ------> func (wg *WaitGroup) Wait()
When passing a non-pointer value to a pointer receiver method, the compiler automatically takes its address (the pointer to the value). Therefore, even though wg is declared as a variable, all three functions are still called on a pointer to wg.
The main reason for using pointer receivers is to avoid unnecessary copying of data. By passing a pointer, the function can modify the underlying WaitGroup directly, rather than making a copy of it. This improves performance, especially for frequently modified WaitGroups.
In the provided code, it is crucial to pass the address of wg to worker because if it was passed as a value (without the &), the Done function in each worker would refer to a different pointer than Add and Wait in the main function. This would result in incorrect behavior.
The above is the detailed content of Why are WaitGroup\'s Add, Done, and Wait functions called using a pointer even when declared as a variable?. For more information, please follow other related articles on the PHP Chinese website!