-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (49 loc) · 786 Bytes
/
main.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
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
for i := 0; i < 10; i++ {
go f(0)
}
var input string
_, err := fmt.Scanln(&input)
if err != nil {
return
}
var c chan string = make(chan string, 5) // buffer size of 5. Making the chan async
go pinger(c)
go ponger(c)
go printer(c)
_, err = fmt.Scanln(&input)
if err != nil {
return
}
// next
}
func f(n int) {
for i := 0; i < 10; i++ {
fmt.Println(n, ":", i)
amt := time.Duration(rand.Intn(255))
time.Sleep(time.Millisecond * amt)
}
}
func pinger(c chan<- string) {
for i := 0; ; i++ {
c <- "ping"
}
}
func printer(c <-chan string) {
for {
msg := <-c
fmt.Println(msg)
time.Sleep(time.Second * 1)
}
}
func ponger(c chan<- string) {
for i := 0; ; i++ {
c <- "pong"
}
}