diff --git a/examples/waitgroups/waitgroups.go b/examples/waitgroups/waitgroups.go index 2cf16a3..c3c8580 100644 --- a/examples/waitgroups/waitgroups.go +++ b/examples/waitgroups/waitgroups.go @@ -13,14 +13,14 @@ import ( // Note that a WaitGroup must be passed to functions by // pointer. func worker(id int, wg *sync.WaitGroup) { + // On return, notify the WaitGroup that we're done. + defer wg.Done() + fmt.Printf("Worker %d starting\n", id) // Sleep to simulate an expensive task. time.Sleep(time.Second) fmt.Printf("Worker %d done\n", id) - - // Notify the WaitGroup that this worker is done. - wg.Done() } func main() { diff --git a/examples/waitgroups/waitgroups.hash b/examples/waitgroups/waitgroups.hash index f944ca9..ab9311b 100644 --- a/examples/waitgroups/waitgroups.hash +++ b/examples/waitgroups/waitgroups.hash @@ -1,2 +1,2 @@ -39bbc00ecd87888761d480666e95d8b2c2a2a589 -oBOGrV0n2Y2 +fd77f5122e6df1669c0a2e0d2c4dfbd30631c21f +7mWXl0yVe6I diff --git a/public/waitgroups b/public/waitgroups index e9d39c7..e47765d 100644 --- a/public/waitgroups +++ b/public/waitgroups @@ -42,7 +42,7 @@ use a wait group.
package main
func worker(id int, wg *sync.WaitGroup) {
- fmt.Printf("Worker %d starting\n", id)
+
On return, notify the WaitGroup that we’re done.
+ + defer wg.Done()
+
fmt.Printf("Worker %d starting\n", id)
time.Sleep(time.Second)
fmt.Printf("Worker %d done\n", id)
-
Notify the WaitGroup that this worker is done.
- - wg.Done()
}