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 操作发生时,触发相应的动作。

基本用法

//select基本用法
select {
case <- chan1:
// 如果chan1成功读到数据,则进行该case处理语句
case chan2 <- 1:
// 如果成功向chan2写入数据,则进行该case处理语句
default:
// 如果上面都没有成功,则进入default处理流程

官方执行步骤

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左侧短变量声明或赋值未被评估。

  1. 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.If 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中的语句

案例分析

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

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.\n", time.Since(start))case <- ch1:fmt.Printf("ch1 case...")case <- ch2:fmt.Printf("ch1 case...")default:fmt.Printf("default go...")}

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

Blocking on read...
default go...
Process finished with exit code 0

修改代码,将default注释:

//default://       fmt.Printf("default go...")

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

Blocking on read...
ch2 case...
Process finished with exit code 0

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

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

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

Blocking on read...
Unblocked 4.000612584s later.
Process finished with exit code 0

示例2 所有channel表达式都会被求值、所有被发送的表达式都会被求值。求值顺序:自上而下、从左到右.

var ch1 chan int
var ch2 chan int
var chs = []chan int{ch1, ch2}
var numbers = []int{1, 2, 3, 4, 5}func main () {select {case getChan(0) <- getNumber(2):fmt.Println("1th case is selected.")case getChan(1) <- getNumber(3):fmt.Println("2th case is selected.")default:fmt.Println("default!.")}}func getNumber(i int) int {fmt.Printf("numbers[%d]\n", i)return numbers[i]
}
func getChan(i int) chan int {fmt.Printf("chs[%d]\n", i)return chs[i]
}

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

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

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

chs[0]
numbers[2]
chs[1]
numbers[3]
default!.Process finished with exit code 0

示例3 break关键字结束select

ch1 := make(chan int, 1)ch2 := make(chan int, 1)ch1 <- 3ch2 <- 5select {case <- ch1:fmt.Println("ch1 selected.")breakfmt.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 breakProcess finished with exit code 0

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

golang select关键字用法相关推荐

  1. oracle 查询关键字,Oracle 中的SELECT 关键字(查询、检索)

    1. SELECT 关键字用法: 检索单个列:select 列名 from 表名: 例:select ename from emp; 检索多个列: select [列1,列2, ... ,列N] fr ...

  2. mysql常见关键字的用法_MySQL 常用关键字用法详解

    MySQL 常用关键字用法详解 在开发工程中,操作数据库的时候经常会有不同类型的条件查询,除了使用where外,Mysql本身也提供了很多常用的关键字.本文主要介绍一些常用的关键字,像update.i ...

  3. C语言中typeof作用,浅析C语言中typeof关键字用法

    浅析C语言中typeof关键字用法 前言 C语言中 typeof 关键字是用来定义变量数据类型的.在linux内核源代码中广泛使用. 下面是Linux内核源代码中一个关于typeof实例: #defi ...

  4. oracle 默认escape_ORACLE中ESCAPE关键字用法

    ORACLE 中 ESCAPE 关键字用法 2012-09-19 00:00 比特网悠虎 关键字: 定义: escape 关键字经常用于使某些特殊字符,如通配符: '%','_' 转义为它们原来的 字 ...

  5. oracle怎么查询关键字,Oracle 中的SELECT 关键字(查询、检索)

    使用通配符的缺点:降低检索的性能 3. 别名:(B) 给表取别名(类似对象名): 形如:select e.ename , e.sal from emp e; 给列取别名(用于显示): 方式一:列名后面 ...

  6. mysql select语句详解_mysql学习笔记之完整的select语句用法实例详解

    本文实例讲述了mysql学习笔记之完整的select语句用法.分享给大家供大家参考,具体如下: 本文内容: 完整语法 去重选项 字段别名 数据源 where group by having order ...

  7. php全局变量的关键字,PHP变量作用域(全局变量局部变量)globalstatic关键字用法实例分析...

    本文实例讲述了PHP变量作用域(全局变量&局部变量)&global&static关键字用法.分享给大家供大家参考,具体如下: 我们知道,变量呢,其实就相当于我们用来储存信息的容 ...

  8. SQL语句 SELECT LIKE用法详解

    在SQL结构化查询语言中,LIKE语句有着至关重要的作用. LIKE语句的语法格式是:select * from 表名 where 字段名 like 对应值(子串),它主要是针对字符型字段的,它的作用 ...

  9. mysql select 使用_mysql select简单用法

    mysql select简单用法 1.select语句可以用回车分隔 $sql=select * from article where id=1 和 $sql=select * from articl ...

最新文章

  1. P1972 [SDOI2009]HH的项链(离线树状数组)
  2. kmp算法详解php,php中字符串匹配KMP算法实现例子
  3. python优先级排序_Python实现一个优先级队列的方法
  4. 父div高度和宽度的应用
  5. 【线段树】FREQUENT - Frequent values(luogu-SP1684 / poj 3368)
  6. 列出所有子集----------2013年1月3日
  7. c++ string substr_用std::string_view替换leveldb的Slice
  8. linux centos history 查看命令历史 显示时间戳
  9. ELK快速搭建日志平台(基于7.9.3)
  10. JDBC连接数据库问题之jdk1.9以及jdk10以后版本的ext问题(添加扩展包问题)
  11. 《圈子圈套3》读后感
  12. 计算机为何引入16进制,计算机内存地址为什么要用16进制数来表示
  13. 计算机用户名怎么注册,微软账号怎么注册 Microsoft帐户注册使用教程
  14. 一、Vulkan开发理论基础知识
  15. 重新开始学Java——java中的标识符(biao,zhi,fu)、关键字和代码规范
  16. 计算机桌面壁纸怎么来回换,我的电脑为什么在总是自动换桌面壁纸???
  17. VMware vSphere的相关知识
  18. 热敏电阻VS模拟温度传感器
  19. 安全集成服务资质是什么都有哪些等级?申请安全集成服务资质认证有什么好处?
  20. 转载--成功应聘Intel的真实经历

热门文章

  1. C 语言编程 — 基本语法
  2. CentOS7 通过 YUM 升级 VIM8
  3. 如何理解写文档这件事情 ?
  4. 同构多核和异构多核简单介绍
  5. Vue中用TypeScript改写JavaScript及装饰器使用
  6. Spring Boot中普通类获取Spring容器中的Bean
  7. ZTE物联网之智能电表简介
  8. Filter的生命周期
  9. 什么是事务(transaction)?它有什么好处
  10. cobertura-maven-plugin