golang:select关键字用法

select是go语言中常用的一个关键字,其用法也一直被用作面试题来考核应聘者。今天,结合代码来分析下select的主要用法。

首先,我们来从官方文档看一下有关select的描述:

A "select" statement chooses which of a set of possible send or receive operations will proceed. It looks similar to a "switch" statement but with the cases all referring to communication operations.
一个select语句用来选择哪个case中的发送或接收操作可以被立即执行。它类似于switch语句,但是它的case涉及到channel有关的I/O操作。

或者换一种说法,select就是用来监听和channel有关的IO操作,当 IO 操作发生时,触发相应的动作。

// communication: a send or receive operation om some  channel

基本用法

select { //不停的在这里检测
    case <-chanl : //检测有没有数据可以读
    //如果chanl成功读取到数据,则进行该case处理语句
    case chan2 <- 1 : //检测有没有可以写
    //如果成功向chan2写入数据,则进行该case处理语句
    
    
    //假如没有default,那么在以上两个条件都不成立的情况下,就会在此阻塞//一般default会不写在里面,select中的default子句总是可运行的,因为会很消耗CPU资源
    default:
    //如果以上都没有符合条件,那么则进行default处理流程
}

在一个select语句中,Go会按顺序从头到尾评估每一个发送和接收的语句。

如果其中的任意一个语句可以继续执行(即没有被阻塞),那么就从那些可以执行的语句中任意选择一条来使用。

如果没有任意一条语句可以执行(即所有的通道都被阻塞),那么有两种可能的情况:

①  如果给出了default语句,那么就会执行default的流程,同时程序的执行会从select语句后的语句中恢复。

②  如果没有default语句,那么select语句将被阻塞,直到至少有一个case可以进行下去。

官方执行步骤

Execution of a "select" statement proceeds in several steps:

1. For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement. The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated.
所有channel表达式都会被求值、所有被发送的表达式都会被求值。求值顺序:自上而下、从左到右.
结果是选择一个发送或接收的channel,无论选择哪一个case进行操作,表达式都会被执行。RecvStmt左侧短变量声明或赋值未被评估。

2. If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.
如果有一个或多个IO操作可以完成,则Go运行时系统会随机的选择一个执行,否则的话,如果有default分支,则执行default分支语句,如果连default都没有,则select语句会一直阻塞,直到至少有一个IO操作可以进行.

3. Unless the selected case is the default case, the respective communication operation is executed.
除非所选择的情况是默认情况,否则执行相应的通信操作。

4.I f the selected case is a RecvStmt with a short variable declaration or an assignment, the left-hand side expressions are evaluated and the received value (or values) are assigned.
如果所选case是具有短变量声明或赋值的RecvStmt,则评估左侧表达式并分配接收值(或多个值)。

5.The statement list of the selected case is executed.
执行所选case中的语句

文档链接: https://golang.org/ref/spec#Select_statements

示例分析

示例1.  如果有一个或多个IO操作可以完成,则Go运行时系统会随机的选择一个执行; 否则的话,如果有default分支,则执行default分支语句,如果连default都没有,则select语句会一直阻塞,直到至少有一个IO操作可以进行

package main

import (
    "fmt"
    "time"
)

func main() {
    start := time.Now()
    c := make(chan interface{})
    ch1 := make(chan int)
    ch2 := make(chan int)

    go func() {
        time.Sleep(4 * time.Second)
        close(c)
    }()

    go func() {
        time.Sleep(3 * time.Second)
        ch1 <- 3
    }()

    go func() {
        time.Sleep(3 * time.Second)
        ch2 <- 5
    }()

    fmt.Println("Blocking on read...")
    select {
    case <-c:
        fmt.Printf("Unblocked %v later.
", time.Since(start))
    case <-ch1:
        fmt.Printf("ch1 case...
")
    case <-ch2:
        fmt.Printf("ch2 case...
")
    default:
        fmt.Printf("default go...
")
    }
}

运行上述代码,由于当前时间还未到3s。所以,目前程序会走default。

/*
NEOdeMacBook-Pro:select_multiplexing_oct_20 zhenxink$ go run blog_select01.go 
Blocking on read...
default go...
NEOdeMacBook-Pro:select_multiplexing_oct_20 zhenxink$ 
*/

修改上述代码,将default注释:

这时,select语句会阻塞,直到监测到一个可以执行的IO操作为止。这里,先会执行完睡眠3s的gorountine,此时两个channel都满足条件,这时系统会随机选择一个case继续操作。

// 运行3 秒后:

/*
NEOdeMacBook-Pro:select_multiplexing_oct_20 zhenxink$ go run blog_select01.go 
Blocking on read...
ch2 case...
NEOdeMacBook-Pro:select_multiplexing_oct_20 zhenxink$ 
*/

接着,继续修改代码,将ch1 和 ch2 的gorountine休眠时间改为5s:

go func() {
        time.Sleep(5*time.Second)
        ch1 <- 3
    }()
go func() { time.Sleep(
5*time.Second) ch2 <- 3 }()

此时会先执行到上面的gorountine,select执行的就是c的case。

/*
NEOdeMacBook-Pro:select_multiplexing_oct_20 zhenxink$ go run blog_select01.go 
Blocking on read...
Unblocked 4.00300947s later.
NEOdeMacBook-Pro:select_multiplexing_oct_20 zhenxink$ 
*/
示例2.  所有channel表达式都会被求值、所有被发送的表达式都会被求值。求值顺序:自上而下、从左到右.
 1 package main
 2 
 3 import "fmt"
 4 
 5 var ch1 chan int
 6 var ch2 chan int
 7 var chs = []chan int{ch1, ch2}
 8 var numbers = []int{1, 2, 3, 4, 5}
 9 
10 func main() {
11 
12     select {
13     case getChan(0) <- getNumber(2):
14         fmt.Println("1st case is selected.")
15     case getChan(1) <- getNumber(3):
16         fmt.Println("2nd case is selected.")
17     default:
18         fmt.Println("default!.")
19     }
20 }
21 
22 func getNumber(i int) int {
23     fmt.Printf("numbers[%d]
", i)
24     return numbers[i]
25 }
26 
27 func getChan(i int) chan int {
28     fmt.Printf("chs[%d]
", i)
29     return chs[i]
30 }

此时,select语句走的是default操作。但是这时每个case的表达式都会被执行。以case1为例:

case getChan(0) <- getNumber(2):

// ch1 <- 3 ,会 block住,因为无缓存的channel 是 synchronous 的,send 和 receive 必须同时满足 

系统会从左到右先执行getChan函数打印chs[0],然后执行getNumber函数打印numbers[2]。同样,从上到下分别执行所有case的语句。所以,程序执行的结果为:

/*
NEOdeMacBook-Pro:select_multiplexing_oct_20 zhenxink$ go run blog_select02.go 
chs[0]
numbers[2]
chs[1]
numbers[3]
default!.
NEOdeMacBook-Pro:select_multiplexing_oct_20 zhenxink$ 
*/

示例3. break关键字结束select

package main

import "fmt"

func main() {
    ch1 := make(chan int, 1)
    ch2 := make(chan int, 1)

    ch1 <- 3
    ch2 <- 5

    select {
    case <-ch1:
        fmt.Println("ch1 selected.")

        break

        fmt.Println("ch1 selected after break")
    case <-ch2:
        fmt.Println("ch2 selected.")
        fmt.Println("ch2 selected without break")
    }
}

很明显,ch1和ch2两个通道都可以读取到值,所以系统会随机选择一个case执行。我们发现选择执行ch1的case时,由于有break关键字只执行了一句:

ch1 selected.

// Process finished with exit code 0

但是,当系统选择ch2的case时,打印结果为:

ch2 selected.
ch2 selected without break

// Process finished with exit code 0

如此就显而易见,break关键字在select中的作用。

参考链接:

https://www.jianshu.com/p/2a1146dc42c3

https://www.cnblogs.com/wt645631686/p/9677868.html

原文地址:https://www.cnblogs.com/neozheng/p/13855757.html