https://nanxiao.gitbooks.io/golang-101-hacks/content/posts/select-operation.html

---------------------------------------------------------------------------------

Select operation


Go's select operation looks similar to switch, but it's dedicated to poll send and receive operations channels. Check the following example:

package main

import (
"fmt"
"time"
) func main() {
ch1 := make(chan int)
ch2 := make(chan int) go func(ch chan int) { <-ch }(ch1)
go func(ch chan int) { ch <- 2 }(ch2) time.Sleep(time.Second) for {
select {
case ch1 <- 1:
fmt.Println("Send operation on ch1 works!")
case <-ch2:
fmt.Println("Receive operation on ch2 works!")
default:
fmt.Println("Exit now!")
return
}
}
}

The running result is like this:

Send operation on ch1 works!
Receive operation on ch2 works!
Exit now!

The select operation will check which case branch can be run, that means the send or receive action can be executed successfully. If more than one case are ready now, the select will randomly choose one to execute. If no case is ready, but there is a default branch, then the default block will be executed, else the select operation will block. In the above example, if the main goroutine doesn't sleep (time.Sleep(time.Second)), the other 2 func goroutines won't obtain the opportunity to run, so only default block in select statement will be executed.

The select statement won't process nil channel, so if a channel used for receive operation is closed, you should mark its value as nil, then it will be kicked out of the selection list. So a common pattern of selection on multiple receive channels looks like this:

for ch1 != nil && ch2 != nil {
select {
case x, ok := <-ch1:
if !ok {
ch1 = nil
break
}
......
case x, ok := <-ch2:
if !ok {
ch2 = nil
break
}
......
}
}

golang中select case 的用途到底是啥的更多相关文章

  1. Golang中Label的用法

    在Golang中能使用Label的有goto, break, continue.,这篇文章就介绍下Golang中Label使用和注意点. 注意点: Label在continue, break中是可选的 ...

  2. select 中使用 case when 和 replace

    在SELECT中,用CASE   例如:     select   a.Cname   as   Tcomname,b.Cname   as   TGoodname,D.nQuanty,c.cNote ...

  3. shell中select、case的使用

    case和select结构在技术上说并不是循环, 因为它们并不对可执行代码块进行迭代. 但是和循环相似的是, 它们也依靠在代码块顶部或底部的条件判断来决定程序的分支. select   select结 ...

  4. golang中,unsafe.sizeof到底是干嘛的?

    https://www.golangtc.com/t/5ad833404ce40d2654053485 小生初学Go,有一点不懂,今天为了知道空结构体到底占多大的空间的时候,去百度说用unsafe.s ...

  5. [Go] 理解 golang 中的 nil

    nil是什么 相信写过Golang的程序员对下面一段代码是非常非常熟悉的了: if err != nil { // do something.... } 当出现不等于nil的时候,说明出现某些错误了, ...

  6. golang的select实现原理剖析

    写在最前面 select为golang提供了多路IO复用机制,和其他IO复用一样,用于检测是否有读写事件是否ready. 本文将介绍一下golang的select的用法和实现原理. 实现原理 gola ...

  7. 【协作式原创】查漏补缺之Golang中mutex源码实现(预备知识)

    预备知识 CAS机制 1. 是什么 参考附录3 CAS 是项乐观锁技术,当多个线程尝试使用 CAS 同时更新同一个变量时,只有其中一个线程能更新变量的值,而其它线程都失败,失败的线程并不会被挂起,而是 ...

  8. golang中的socket编程

    0.1.索引 https://waterflow.link/articles/1664591292871 1.tcp的3次握手(建立连接) 客户端的协议栈向服务器端发送了 SYN 包,并告诉服务器端当 ...

  9. golang中channel的超时处理

    并发中超时处理是必不可少的,golang没有提供直接的超时处理机制,但可以利用select机制来解决超时问题. func timeoutFunc() { //首先,实现并执行一个匿名的超时等待函数 t ...

随机推荐

  1. Winform学习知识汇总

    引用博客 http://www.cnblogs.com/peterzb/archive/2009/06/14/1502918.html

  2. Angular JS中自定义标签 属性绑定的解释

    看到自定义标签的文档时,文档作者解释的能力实在太弱,也可能是本人太笨,一下绕不过来. 看了一个stackoverflow答案,才算明白,在此贴出翻译,以供大家参考. .csharpcode, .csh ...

  3. 【sqli-labs】 less65 GET -Challenge -Blind -130 queries allowed -Variation4 (GET型 挑战 盲注 只允许130次查询 变化4)

    双引号括号闭合 http://192.168.136.128/sqli-labs-master/Less-65/?id=1")%23

  4. MySql(四)Select条件查询

    select条件查询的格式如下: SELECT 查询列表FROM 表名WHERE 筛选条件:123456根据筛选条件可以分为以下几类: 按照条件按表达式进行筛选 常用条件运算符如下:> .< ...

  5. radiobutton group

    1. 环境:VS2010 2. 分组 将radio1.radio2.radio3分为1组,radio4.radio5分为另一组: 方法:设置  radio1  的 属性:  group.tabstop ...

  6. vuex状态管理demo

    vuex状态管理主要包含四个概念  mapState,mapMutations,mapGetters,mapActions. 编写vuex文件夹下面的store.js import Vue from ...

  7. C#读取文件-古文观止(总结一下)

    1,读取单个文件 //读取一个文本文件 private void buttonRead_Click(object sender, EventArgs e) { String path = Enviro ...

  8. mess系统 开发技术,需求整理

    1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源(如html 页 ...

  9. 匹配 C 语言样式字符串

    #include <stdio.h> char haha[] = "nihaoma" "niubi" "\"hello worl ...

  10. 恶补数论(二) Baby-Step-Giant-Step 大步小步求离散模对数

    知识概述 好吧,我承认这是我初三寒假就听过的知识,然而我现在早就高一了(又是寒假,只不过我已经在省选了...) 额,这是求离散模对数的一种算法 也就是求满足方程a^x≡b(mod p)的最小的x(其中 ...