-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannels.go
53 lines (42 loc) · 797 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
package main
import "fmt"
func main() {
c := make(chan int)
chs := make(chan string, 5)
inp := make(chan string)
out := make(chan string)
go task(c)
c <- 5
go task2(chs, "test")
fmt.Println(<-c)
fmt.Println(<-chs)
fmt.Println(<-chs)
fmt.Println(<-chs)
fmt.Println(<-chs)
go removeDuplicates(inp, out)
inp <- "DODO"
fmt.Println(<-out)
inp <- "DODt"
fmt.Println(<-out)
}
func task(ch chan int) {
n := <-ch
ch <- n + 1
}
func task2(ch chan string, str string) {
ch <- str + " "
ch <- str + " 1"
ch <- str + " 2"
ch <- str + " 3"
ch <- str + " 4"
}
func removeDuplicates(inputStream chan string, outputStream chan string) {
defer close(outputStream)
var str, tmp string
for str = range inputStream {
if str != tmp {
tmp = str
outputStream <- str
}
}
}