go select code example
Example: select channel golang
package main
import (
"fmt"
)
func main() {
c := make(chan int)
go sender(c)
receiver(c)
}
func sender(c chan<- int) {
defer close(c)
for i := 0; i < 10; i++ {
c <- i
}
}
func receiver(c <-chan int) {
for {
select {
case v, ok := <-c:
if !ok {
return
}
fmt.Println(v)
}
}
}