GO语言:goroutine与select

87 阅读1分钟
package main

import (
	"fmt"
	"time"
)

func main() {
	a := make(chan bool)
	b := make(chan bool)

	go func(isRun chan bool) {
		time.Sleep(time.Second * 10)
		isRun <- true
	}(a)

	go func(isRun chan bool) {
		time.Sleep(time.Second * 5)
		isRun <- true
	}(b)

	select {
	case <-a:
		fmt.Println("run a")
	case <-b:
		fmt.Println("run b")
	}

}
1.select与switch很像
2.select一般和通道使用
3.select会判断所有的case,哪个case最先出结果就执行哪个
4.一样快,谁先检测到就执行谁