Standard library / sync
sync.WaitGroup.Add()
Change the task counter. Add positive counts before starting goroutines when the counter is zero; negative counters panic. Expected stdout: '7\n'.
Formal Syntax & Structure
go syntaxgo
func (wg *WaitGroup) Add(delta int)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| delta | int | Amount to add; for WaitGroup, change in outstanding task count. |
Working Example
Example Snippetgo
package main
import ( "fmt" "sync")
func main() { var w sync.WaitGroup w.Add(1) done := make(chan int, 1) go func() { defer w.Done(); done <- 7 }() w.Wait() fmt.Println(<-done)}