30 lines
415 B
Go
30 lines
415 B
Go
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)
|
|
}
|