Created
February 27, 2020 23:15
-
-
Save mannion007/017d31e6bd2b6d5ebc30423f5fb867f2 to your computer and use it in GitHub Desktop.
Select statement for channel
This file contains 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 main() { | |
q := make(chan bool) | |
c := gen(q) | |
receive(c, q) | |
fmt.Println("about to exit") | |
} | |
func gen(q chan<- bool) <-chan int { | |
c := make(chan int) | |
go func() { | |
for i := 0; i < 100; i++ { | |
c <- i | |
} | |
close(c) | |
q <- true | |
}() | |
return c | |
} | |
func receive(c <-chan int, q <-chan bool) { | |
x := 0 | |
for { | |
select { | |
case x = <-c: | |
fmt.Println(x) | |
case <-q: | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment