Added waitgroups practice exercise

Yes, I am back
This commit is contained in:
raul 2024-10-11 12:59:02 +02:00
parent 4b2777ea91
commit 1e08dc4069
2 changed files with 32 additions and 0 deletions

3
waitgroups/go.mod Normal file
View File

@ -0,0 +1,3 @@
module waitgroups
go 1.23.1

29
waitgroups/main.go Normal file
View File

@ -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)
}