-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_chan.go
62 lines (56 loc) · 1.04 KB
/
simple_chan.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
62
package example
import "fmt"
//glog.Logger()
func sum(s []int, c chan int, str string) {
sum := 0
for _, v := range s {
sum += v
}
fmt.Printf("%s, sum is %d\n", str, sum)
c <- sum
}
func Gchan() {
data := []int{1, 2, 3, 7, -8, -2}
c := make(chan int)
go sum(data[:len(data)/2], c, "first thread")
go sum(data[len(data)/2:], c, "second thread")
x, y := <-c, <-c
fmt.Println(x, y, x+y)
// buffered channel
bc := make(chan string, 5)
bc <- "hello world"
bc <- "buffered channel for go"
// a little interesting here, with one "<-bc" couldn't receive two sends from buffered channel
fmt.Println(<-bc)
fmt.Println(<-bc)
close(bc)
v, ok := <-bc
if !ok {
fmt.Println("channel closed")
} else {
fmt.Println(v)
}
}
func sel(in, quit chan int) {
x, y := 0, 1
for {
select {
case in <- x:
x, y = y, x+y
case <-quit:
fmt.Println("Quit received")
return
}
}
}
func Gsel() {
in := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<-in)
}
quit <- 0
}()
sel(in, quit)
}