source:
doc/papers/concurrency/examples/channels.go@
13d326ec
Last change on this file since 13d326ec was 2aab69b, checked in by , 6 years ago | |
---|---|
|
|
File size: 786 bytes |
Rev | Line | |
---|---|---|
[2aab69b] | 1 | package main |
2 | import "fmt" | |
3 | func main() { | |
4 | type Msg struct{ i, j int } | |
5 | ||
6 | ch1 := make( chan int ) | |
7 | ch2 := make( chan float32 ) | |
8 | ch3 := make( chan Msg ) | |
9 | hand := make( chan string ) | |
10 | shake := make( chan string ) | |
11 | gortn := func() { // thread starts | |
12 | var i int; var f float32; var m Msg | |
13 | L: for { | |
14 | select { // wait for message | |
15 | case i = <- ch1: fmt.Println( i ) | |
16 | case f = <- ch2: fmt.Println( f ) | |
17 | case m = <- ch3: fmt.Println( m ) | |
18 | case <- hand: break L // sentinel | |
19 | } | |
20 | } | |
21 | shake <- "SHAKE" // completion | |
22 | } | |
23 | ||
24 | go gortn() // start thread | |
25 | ch1 <- 0 // different messages | |
26 | ch2 <- 2.5 | |
27 | ch3 <- Msg{1, 2} | |
28 | hand <- "HAND" // sentinel value | |
29 | <- shake // wait for completion | |
30 | } | |
31 | ||
32 | // Local Variables: // | |
33 | // tab-width: 4 // | |
34 | // compile-command: "go run channels.go" // | |
35 | // End: // |
Note:
See TracBrowser
for help on using the repository browser.