Skip to content

Latest commit

 

History

History
38 lines (30 loc) · 904 Bytes

File metadata and controls

38 lines (30 loc) · 904 Bytes

Listing E.3: WaitGroup

Code in the file

Tip

Click the links to see the file and its directory in their original locations and state as they were at the time of the listing.

package main

import (
	"fmt"
	"math/rand/v2"
	"sync"
	"time"
)

func work(id int) {
	time.Sleep(rand.N(10 * time.Second))
	fmt.Printf("worker %d done.", id)
}

func main() {
	var wg sync.WaitGroup
	for id := range 10 {
		wg.Add(1)
		go func() {
			defer wg.Done()
			work(id + 1)
		}()
	}
	wg.Wait()
	fmt.Print("main done.")
}