使用 Select
如果此时我们有多个 channel,我们想从多个 channel 接收数据,谁来的快先输出谁,此时应该怎么做呢?答案是使用 select:
package main
import "fmt"
func main() {
var c1, c2 chan int // c1 and c2 == nil
//n1 := <-c1
//n2 := <-c2
// 如果我们想同时从 c1 和 c2 接收数据, 谁来的快
// 就先要谁, 此时就需要使用 select 来帮助我们完成上述任务
select {
case n := <-c1:
fmt.Println("Received from c1:", n)
case n := <-c2:
fmt.Println("Received from c2:", n)
default:
fmt.Println("No value received")
}
}
此时程序执行将会输出 default 的内容。为 select 加上一个无限循环,将会一直输出 default 的内容,但是如果去掉 default,将会报错:
这个错误在说的是,现在没有协程启动并向 channel 当中发送数据。
现在我们新建一个生产 channel 的工厂函数 generator,在 generator 当中,我们开启一个 goroutine,在 goroutine 当中我们开启一个无限循环,每一次迭代随机 sleep 一定时间,并将不断自增的 i i i输入到 channel 当中:
func generator() chan int {
out := make(chan int)
go func() {
i := 0
for {
time.Sleep(
time.Duration(rand.Intn(1500)) * time.Millisecond)
out <- i
i++
}
}()
return out
}
此时修改 main 函数为:
func main() {
c1, c2 := generator(), generator()
//n1 := <-c1
//n2 := <-c2
// 如果我们想同时从 c1 和 c2 接收数据, 谁来的快
// 就先要谁, 此时就需要使用 select 来帮助我们完成上述任务
for {
select {
case n := <-c1:
fmt.Println("Received from c1:", n)
case n := <-c2:
fmt.Println("Received from c2:", n)
}
}
}
得到的结果如下:
Received from c2: 0
Received from c2: 1
Received from c1: 0
Received from c1: 1
Received from c2: 2
Received from c1: 2
Received from c1: 3
Received from c2: 3
Received from c2: 4
... ...
一个更复杂的使用 select 的例子如下,下面的例子对上述代码进行了修改,加入了定时器,并使用 createWorker 和 worker 不断地开启 goroutine 并向 channel 发送数据:
package main
import (
"fmt"
"math/rand"
"time"
)
func generator() chan int {
out := make(chan int)
go func() {
i := 0
for {
time.Sleep(
time.Duration(rand.Intn(1500)) * time.Millisecond)
out <- i
i++
}
}()
return out
}
func worker(id int, c chan int) {
for n := range c {
time.Sleep(time.Second)
fmt.Printf("Worker %d received %d\n",
id, n)
}
}
func createWorker(id int) chan<- int {
c := make(chan int)
go worker(id, c)
return c
}
func main() {
var c1, c2 = generator(), generator()
var worker = createWorker(0)
var values []int
tm := time.After(10 * time.Second)
tick := time.Tick(time.Second)
for {
var activeWorker chan<- int
var activeValue int
if len(values) > 0 {
activeWorker = worker
activeValue = values[0]
}
select {
case n := <-c1:
values = append(values, n)
case n := <-c2:
values = append(values, n)
case activeWorker <- activeValue:
values = values[1:]
case <-time.After(800 * time.Millisecond):
// 如果每两次生成数据之间的时间超过了 800ms:
fmt.Println("timeout")
case <-tick:
fmt.Println("queue len:", len(values))
case <-tm:
fmt.Println("bye")
return
}
}
}