Let's say we are selecting on two channel. After running a few times, one of the channels is closed. How can I break out of the select?
package main
import (
"fmt"
)
func main() {
ch := make(chan int)
closed := make(chan bool, 1)
go func() {
for i := 0; i < 5; i++ {
ch <- 1
}
close(ch)
}()
for {
select {
case v := <-ch:
fmt.Printf("v is %v
", v)
case <-closed:
fmt.Println("The server is closed!")
return
}
}
fmt.Println("Break!")
}
It is blocked infinitely. How to make it work?