From c5904be53b63e0d8451bdb69a22ed705f40bc27c Mon Sep 17 00:00:00 2001 From: Dewald Swanepoel Date: Sun, 20 Mar 2022 11:57:08 +0200 Subject: [PATCH] Fixed bug in WaitGroup example The waitgroup that is defined in main() with var wg sync.WaitGroup, only has scope inside the main() function. The doOperation() function will not know about it and so the defer wg.Done() line will result in a compile time error. The waitgroup needs to be passed as a parameter to the doOperation() function. --- go.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.md b/go.md index c12286210f..1ca0142707 100644 --- a/go.md +++ b/go.md @@ -431,7 +431,7 @@ func main() { for _, item := range itemList { // Increment WaitGroup Counter wg.Add(1) - go doOperation(item) + go doOperation(&wg, item) } // Wait for goroutines to finish wg.Wait() @@ -441,7 +441,7 @@ func main() { {: data-line="1,4,8,12"} ```go -func doOperation(item string) { +func doOperation(wg *sync.WaitGroup, item string) { defer wg.Done() // do operation on item // ...