-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex2.go
61 lines (49 loc) · 1.04 KB
/
ex2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
var meals = []string{"chorizo", "chopitos", "pimientos de padrón", "croquetas", "atatas bravas"}
var friends = []string{"Alice", "Bop", "Charlie", "Dave"}
func main() {
fmt.Println("Bon appétit!")
var wg sync.WaitGroup
for _, friend := range friends {
enjoin(friend, &wg)
}
wg.Wait()
}
func enjoin(name string, wg *sync.WaitGroup) {
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
wg.Add(1)
go func() {
defer wg.Done()
var wg2 sync.WaitGroup
for _, val := range meals {
nums := 5 + r1.Intn(5)
for i := 0; i < nums; i++ {
morse(name, val, &wg2)
}
}
wg2.Wait()
}()
}
func morse(name string, meal string, wg *sync.WaitGroup) {
wg.Add(1)
fmt.Printf("%s is enjoing some %s \n", name, meal)
go func() {
t := seed2()
time.AfterFunc(t, func() {
wg.Done()
})
}()
}
func seed2() time.Duration {
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
t := 30 + r1.Intn(3*60-30)
return time.Duration(t) * time.Millisecond
}