The traditional method while traversing the pipeline, if not closed clog and lead to deadlock, in the actual development of our well established in the pipe when closed. You can use select to resolve.
/**
* @Author: v_bivwei
* @Description:
* @Date: 2020/1/22 18:03
*/
package main
import (
"fmt"
)
func main() {
var intChan = make(chan int, 10)
for i := 0; i < 10; i++ {
intChan <- i
}
var stringChin = make(chan string, 5)
for i := 0; i < 5; i++ {
stringChin <- fmt.Sprintf("stringChin %d", i)
}
for {
select {
case v := <-intChan:
fmt.Printf("intChin 读出来的数据为 %d\n", v)
case v := <-stringChin:
fmt.Printf("intChin 读出来的数据为 %s\n", v)
default:
// 取完了的逻辑
// ...
return
}
}
}