-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannels.go
More file actions
65 lines (54 loc) 路 1.46 KB
/
Copy pathchannels.go
File metadata and controls
65 lines (54 loc) 路 1.46 KB
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
63
64
65
package main
import "fmt"
import "time"
func main() {
ch := make(chan int) // let's make a channel of integers.
/*
below, ch blocks everything because it creates a "deadlock", blocking
anything to get pushed to the channel.
<-ch
fmt.Println("Here")
*/
// try this instead.
go func() {
// send number of the channel...
ch <- 353
}()
// ...then receive from the channel...
val := <-ch
fmt.Printf("got %d\n", val)
fmt.Println("-----")
// ...now let's see if we can send multiple values to the channel with a
// for loop that will send values 0, 1, 2 per second:
go func() {
for i := 0; i < 3; i++ {
fmt.Printf("sending %d\n", i)
ch <- i
time.Sleep(time.Second)
}
}()
// and send those values as they come, to the channel, and print what ch
// received:
for i := 0; i < 3; i++ {
val := <-ch
fmt.Printf("received %d\n", val)
fmt.Println("-----")
// The outcome will be an assurance that we both sended and received each
// value successfully.
// Now let's assume that we don't know how many items we have in the channel.
// We'll signal that the work is done by closing the channel:
go func() {
for i := 0; i < 3; i++ {
fmt.Printf("sending %d\n", i)
ch <- i
time.Sleep(time.Second)
}
close(ch)
}()
// we use the range keyword to iterate over the values, and then print what
// we received.
for i := range ch {
fmt.Printf("received %d\n", i)
}
}
}