parent
4b2777ea91
commit
1e08dc4069
|
@ -0,0 +1,3 @@
|
|||
module waitgroups
|
||||
|
||||
go 1.23.1
|
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var numberList = []int{1, 2, 3, 4, 5}
|
||||
|
||||
func main() {
|
||||
var a sync.WaitGroup
|
||||
|
||||
for _, v := range numberList {
|
||||
a.Add(1)
|
||||
go func() {
|
||||
defer a.Done()
|
||||
worker(v)
|
||||
}()
|
||||
}
|
||||
a.Wait()
|
||||
}
|
||||
|
||||
func worker(id int) {
|
||||
fmt.Printf("Worker %v is starting\n", id)
|
||||
time.Sleep(time.Duration(rand.Intn(10) * int(time.Second)))
|
||||
fmt.Printf("Worker %v is done\n", id)
|
||||
}
|
Loading…
Reference in New Issue