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 ...
随机推荐
- how to do a mass update in Laravel5 ( 在Laravel 5里面怎么做大量数据更新 )
Today, I had spent 3 hours to fix one problem, The old program has a bug, originally, when a user pr ...
- Android Programming 3D Graphics with OpenGL ES (Including Nehe's Port)
https://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html
- org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: Property [xxx] not readable on type [xxx]
由于javaBean中的属性是custFullName,所以在使用jsp的时候,通过el表达式获取属性的值<td>${m.CustFullName}</td>.但是加载页面的时 ...
- windows 下完全卸载service
用SC Delete命令的话,如果服务名称中带空格,则请在服务名称前面用半角的双引号括起,如SC delete "Adobe LM Service",另外Services这个子键一 ...
- Java二分法查找
二分法查找 /** * 二分法查找 找不到返回-1 * @author yangzi * */ public class TwoFind { public static int twoFind(int ...
- centos 搭建pptp
很多朋友不会在CENTOS 在安装vpn,因为对于菜鸟来说安装VPN的确是个头晕的事情,特别是安装openvpn,不过安装pptp就稍微简单一点,国内网上有很多安装pptp的教程,但是都很繁杂,我在国 ...
- Pytorch 加载保存模型【直播】2019 年县域农业大脑AI挑战赛---(三)保存结果
在模型训练结束,结束后,通常是一个分割模型,输入 1024x1024 输出 4x1024x1024. 一种方法就是将整个图切块,然后每张预测,但是有个不好处就是可能在边界处断续. 由于这种切块再预测很 ...
- ssh多主机
#node1 HOST node1 HostName 10.10.10.10 Port 21 User ubuntu UseKeychain yes AddKeysToAgent yes #node2 ...
- luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线
Code: #include<bits/stdc++.h> #define maxn 1200000 #define N 120000 using namespace std; char ...
- P1077摆花
传送 总共要摆m盆花,而每种花最多有a[i]盆.仔细思索,发现它是一个多重背包求方案数问题.但是我蒟蒻的不会,于是跑去问大佬. 以下状态转移方程及化简from rqy 如果第i个物品有a[i],每个的 ...