11.7 并发模式(下)

一、同时等待多个任务第二种写法

使用select去写

func fanInSelect(c1, c2 chan string) chan string {
	c := make(chan string)
	go func() {
		for {
			select {
			case m := <-c1:
				c <- m
			case m := <-c2:
				c <- m
			}
		}
	}()

	return c
}

二、同时等待多个任务选择条件

  1. 已知chan数量,使用select写

  2. 未知chan数量

Last updated

Was this helpful?