Created
January 25, 2018 07:55
-
-
Save kogai/f07be483b293e7cda55e875b634c5bd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ("time") | |
| func Producer(ch chan int) { | |
| for i := 0; i < 5; i++ { ch <- i } | |
| println("close") | |
| close(ch) | |
| } | |
| func Consumer(ch1, _ chan int) { | |
| for { | |
| select { | |
| case x := <- ch1: { | |
| if x > 0 { | |
| println("ch1-", x) | |
| } | |
| } | |
| // case x := <- ch2: println("ch2-", x) | |
| } | |
| } | |
| } | |
| func main() { | |
| ch1, ch2 := make(chan int), make(chan int) | |
| go Producer(ch1) | |
| // go Producer(ch2) | |
| // Consumer(ch1, ch2) | |
| for { | |
| select { | |
| case x := <- ch1: { | |
| if x > 0 { | |
| println("ch1-", x) | |
| } | |
| } | |
| // case x := <- ch2: println("ch2-", x) | |
| } | |
| } | |
| time.Sleep(time.Second * 5) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment