-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannels.go
61 lines (45 loc) · 821 Bytes
/
channels.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"
"time"
)
func printNum(c chan <- int) {
for i := 0; i < 5; i++ {
fmt.Printf("Number added: %d\n", i)
c <- i
time.Sleep(time.Millisecond * 150)
}
close(c)
}
func printLetter(c chan <- string) {
for i := 'a'; i < 'j'; i++ {
fmt.Printf("Letter added: %c\n", i)
c <- string(i)
time.Sleep(time.Millisecond * 100)
}
close(c)
}
func main() {
cn := make(chan int, 10)
cl := make(chan string)
cnEnd := make(chan bool)
clEnd := make(chan bool)
go printLetter(cl)
go printNum(cn)
go func() {
for n := range cn {
fmt.Printf("Number read: %d\n", n)
time.Sleep(time.Millisecond * 350)
}
cnEnd <- true
}()
go func() {
for l := range cl {
fmt.Printf("Letter read: %s\n", string(l))
}
clEnd <- true
}()
<- cnEnd
<- clEnd
fmt.Println()
}