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 ...
随机推荐
- [ TJOI 2007 ] 线段
\(\\\) \(Description\) 一个\(N\times N\) 的网格,每行有一段要必走,求从\((1,1)\)到\((N,N)\)的最短路长度. \(N\le 2\times10^4\ ...
- 语音跟踪:信号分解、锁相、鸡尾酒会效应、基于PR的信号分离
NLP中关于语音的部分,其中重要的一点是语音信号从背景噪音中分离.比如在一个办公室场景中,有白天的底噪-类似于白噪音的噪音.空调的声音.键盘的啪啪声.左手边45度7米元的地方同事讨论的声音.右手边1. ...
- canvas一周一练 -- canvas绘制奥运五环(1)
运行效果: <!DOCTYPE html> <html> <head> </head> <body> <canvas id=" ...
- tcpdump命令使用方法
NAME tcpdump - 转储网络上的数据流 总 tcpdump [ -adeflnNOpqStvx ] [ -c count ] [ -F file ] [ -i interface ] [ - ...
- java虚拟(一)--java内存区域和常量池概念
一.java运行时数据区 也可以称为java内存区域,和java内存模型不是一回事,不要弄混,这里基于jdk1.8之前 1.1.方法区 线程共享,类装载过程中产生的java.lang.Class对象保 ...
- 解决vue项目运行过程中,npm run dev 报错问题
[方案1] 错误如下: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! travel@1.0.0 dev: `webpack-dev-server ...
- CodeFrist基础_迁移更新数据
一丶自动迁移 第一次启用迁移:NeGet-->Enable-Migrations public DemoDbContext() : base("name=ConncodeFirst&q ...
- 数据导出为Excel(未完)
更多详细内容 view页面: function Download() { //多个查询条件 dateStart = $("#j_dataTimeStart").datebox(&q ...
- top命令的用法
top命令的用法 2018年07月15日 09:50:04 zhuoya_ 阅读数:1858 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/z ...
- importdata-- matlab
source file: test.dat *************************** Day1 Day2 Day3 Day4 Day5 Day6 Day795.01 76.2 ...