Skip to content

Instantly share code, notes, and snippets.

@mannion007
Created February 27, 2020 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mannion007/017d31e6bd2b6d5ebc30423f5fb867f2 to your computer and use it in GitHub Desktop.
Save mannion007/017d31e6bd2b6d5ebc30423f5fb867f2 to your computer and use it in GitHub Desktop.
Select statement for channel
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