From 0e9b7337ab3a9f16d08b3808e26f12c99020278b Mon Sep 17 00:00:00 2001 From: Rahul Date: Wed, 29 Jan 2020 18:09:27 +0530 Subject: [PATCH] Update go.md Added the usage of WaitGroup in go --- go.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/go.md b/go.md index 64d78347f7..74c7a969b7 100644 --- a/go.md +++ b/go.md @@ -395,6 +395,39 @@ v, ok := <- ch See: [Range and close](https://tour.golang.org/concurrency/4) +### WaitGroup + +```go +import "sync" + +func main() { + var wg sync.WaitGroup + + for _, item := range itemList { + // Increment WaitGroup Counter + wg.Add(1) + go doOperation(item) + } + // Wait for goroutines to finish + wg.Wait() + +} +``` +{: data-line="1,4,8,12"} + +```go +func doOperation(item string) { + defer wg.Done() + // do operation on item + // ... +} +``` +{: data-line="2"} + +A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. The goroutine calls `wg.Done()` when it finishes. +See: [WaitGroup](https://golang.org/pkg/sync/#WaitGroup) + + ## Error control ### Defer