golang中select case 的用途到底是啥
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 的用途到底是啥的更多相关文章
- Golang中Label的用法
在Golang中能使用Label的有goto, break, continue.,这篇文章就介绍下Golang中Label使用和注意点. 注意点: Label在continue, break中是可选的 ...
- select 中使用 case when 和 replace
在SELECT中,用CASE 例如: select a.Cname as Tcomname,b.Cname as TGoodname,D.nQuanty,c.cNote ...
- shell中select、case的使用
case和select结构在技术上说并不是循环, 因为它们并不对可执行代码块进行迭代. 但是和循环相似的是, 它们也依靠在代码块顶部或底部的条件判断来决定程序的分支. select select结 ...
- golang中,unsafe.sizeof到底是干嘛的?
https://www.golangtc.com/t/5ad833404ce40d2654053485 小生初学Go,有一点不懂,今天为了知道空结构体到底占多大的空间的时候,去百度说用unsafe.s ...
- [Go] 理解 golang 中的 nil
nil是什么 相信写过Golang的程序员对下面一段代码是非常非常熟悉的了: if err != nil { // do something.... } 当出现不等于nil的时候,说明出现某些错误了, ...
- golang的select实现原理剖析
写在最前面 select为golang提供了多路IO复用机制,和其他IO复用一样,用于检测是否有读写事件是否ready. 本文将介绍一下golang的select的用法和实现原理. 实现原理 gola ...
- 【协作式原创】查漏补缺之Golang中mutex源码实现(预备知识)
预备知识 CAS机制 1. 是什么 参考附录3 CAS 是项乐观锁技术,当多个线程尝试使用 CAS 同时更新同一个变量时,只有其中一个线程能更新变量的值,而其它线程都失败,失败的线程并不会被挂起,而是 ...
- golang中的socket编程
0.1.索引 https://waterflow.link/articles/1664591292871 1.tcp的3次握手(建立连接) 客户端的协议栈向服务器端发送了 SYN 包,并告诉服务器端当 ...
- golang中channel的超时处理
并发中超时处理是必不可少的,golang没有提供直接的超时处理机制,但可以利用select机制来解决超时问题. func timeoutFunc() { //首先,实现并执行一个匿名的超时等待函数 t ...
随机推荐
- C#手机充值系统开发(基于聚合数据)
说是手机充值系统有点装了,其实就是调用了聚合数据的支付接口,其实挺简单的事 但是我发现博客园竟然没有类似文章,我就个出头鸟把我的代码贡献出来吧 首先说准备工作: 去聚合数据申请账号-添加手机支付的认证 ...
- 17 C#中的循环执行 while循环
在编程中有代码的执行主要有三种方式.(1)顺序执行,也就是一条语句一条语句按顺序执行:(2)条件执行,也就是if...else.当某种条件满足时执行一些代码:(3)循环执行,就是当某种条件满足的时候, ...
- Docker (1) 基本概念和安装
Docker简介 什么是容器? 一种虚拟化的方案,操作系统级别的虚拟化.容器是一个轻量的.独立的.可执行的包,包含了执行它所需要的所有东西:代码.运行环境.系统工具.系统库.设置.很长一段时间中,容器 ...
- jQuery学习笔记(2)-选择器的使用
一.选择器是什么 有了jQuery的选择器,我们几乎可以获取页面上任意一个或一组对象 二.Dom对象和jQuery包装集 1.Dom对象 JavaScript中获取Dom对象的方式 <div i ...
- Android 滚动RecyclerView加载图片时的流畅度优化
实现:使用onScrollStateChanged回调检测滚动状态,并在RecyclerViewAdapter内部设置类似isScrolling的状态值来控制网络图片的加载. 下面是代码举例: // ...
- git分支拉取
假设你已经配置好了各种SSH Key之类并熟悉基本的git创建分支.提交分支命令.比如共有2个分支,自己在一台未配置origin电脑上想要拉取某个分支(dev)到本地.步骤如下:1.新建git项目 与 ...
- setTimeout 0
setTimeout 0 就是把事件放到下一次事件循环时调用,至少要一个时钟之后: ); console.log('this should before setTimeout 0'); var i=0 ...
- java项目其他基础配置
创建完maven项目之后. 1.pom.xml文件配置项目相关的架包. 2.src.main.resources下边 创建文件夹:spring以及mapper. 3.src.main.resource ...
- Django线上部署教程:腾讯云+Ubuntu+Django+Uwsgi(转载)
网站名称: 向东的笔记本 本文链接: https://www.eastnotes.com/post/29 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议.转载请注明出处! ...
- 第一节:重写(new)、覆写(overwrite)、和重载(overload)
一丶重写<NEW> 子类重写父类方法,方法里加new, eg: public new void CommonMethord1(string msg){} 子类继承父类中的普通方法,如果在子 ...