当前位置:网站首页>golang并发编程之select
golang并发编程之select
2022-06-22 03:08:00 【ManNiaoQinFen】
1.select是Go中的一个控制结构,类似于switch语句,用于处理异步IO操作。select会监听case语句中channel的读写操作,当case中channel读写操作为非阻塞状态(即能读写)时,将会触发相应的动作。
select中的case语句必须是一个channel操作
select中的default子句总是可运行的。
2.如果有多个case都可以运行,select会随机公平地选出一个执行,其他不会执行。
3.如果没有可运行的case语句,且有default语句,那么就会执行default的动作。
4.如果没有可运行的case语句,且没有default语句,select将阻塞,直到某个case通信可以运行
package main
import (
"fmt"
"time"
)
var chanInt = make(chan int)
var chanStr = make(chan string)
func main() {
go func() {
chanInt <- 10
chanStr <- "hello"
close(chanInt)
close(chanStr)
}()
for {
select {
case r := <-chanInt:
fmt.Printf("chanInt: %v\n", r)
case r := <-chanStr:
fmt.Printf("chanStr: %v\n", r)
default:
fmt.Println("default....")
}
time.Sleep(time.Second)
}
}
运行结果
chanInt: 10
chanStr: hello
chanStr:
chanStr:
chanStr:
边栏推荐
- 360edr planing
- BOM 属性、方法、事件应用案例
- tx2镜像源设置
- 背光模组的基本结构与应用
- 为什么在高并发下很容易就被setInterval给坑了
- xpm_ memory_ A complete example of using the tdpram primitive
- uv_loop_init()流程
- Tag dynamic programming - preliminary knowledge for question brushing -1 Dynamic programming five part problem solving method + lt.509 Fibonacci number / Sword finger offer 10 I + lt.70. Climbing stai
- Figure base de données ongdb version V - 1.0.2
- TX2挂载SD卡,jtop
猜你喜欢

HarmonyOS鸿蒙使用ORM Bee访问数据库实例
![[crawler notes 2] mouse events, screenshots and common attack methods](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[crawler notes 2] mouse events, screenshots and common attack methods

Day12QFile2021-09-27

Hosting environment, components, APIs, collaboration and publishing of applets

Typora + picGo 配置图床实现图片自动上传

Markdown is proficient in Elementary Grammar and is compatible with marktext

为什么在高并发下很容易就被setInterval给坑了

Opencv installation (x86/tx2 cuda/ shared library)

什么是SSL证书,拥有一个SSL证书有什么好处?

Harmonyos Hongmeng uses ORM bee to access database instances
随机推荐
Classification of traffic signs
[crawler notes 1] environment construction and necessary tools selenium
selenium入门级项目 - 豆豆玩竞猜
记一则服务器内存泄漏解决过程
uv_ Run process
[percona toolkit] series of Pt table checksum and Pt table sync data verification and repair artifacts
【爬虫笔记2】鼠标事件与截图方法、常用攻击方法
ZCMU--1052: Holedox Eating(C语言)
Moorish voting
[summary of leetcode weekly competition] summary of the 298th weekly competition of leetcode (6.19)
图数据平台解决方案:单节点部署
Redis6.0 new features (Part 2)
Markdown advanced syntax, marktext compatible
Opencv installation (x86/tx2 cuda/ shared library)
[nvme2.0b 6] nvme queue model
uv_loop_init()流程
Force buckle 295 Median data flow
Classic case of JS operation node (three-level linkage)
你是一名技术管理者还是项目管理者?
table标签的不规则布局