Created
October 25, 2017 01:32
-
-
Save krishna2nd/27fae7da220eae3491b74ebdfce286a3 to your computer and use it in GitHub Desktop.
Go dual channels
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 ( | |
| "fmt" | |
| ) | |
| func printch(ch1, ch2, q chan int) { | |
| for { | |
| select { | |
| case x := <-ch1: | |
| fmt.Print(x) | |
| case y := <-ch2: | |
| fmt.Print(y) | |
| case <-q: | |
| fmt.Println("quiting..") | |
| return | |
| } | |
| fmt.Println() | |
| } | |
| } | |
| func main() { | |
| var ch1, ch2, q = make(chan int), make(chan int), make(chan int) | |
| go func() { | |
| for i := 0; i < 10; i++ { | |
| ch2 <- i * i | |
| ch1 <- i | |
| } | |
| q <- 0 | |
| }() | |
| printch(ch1, ch2, q) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment