当前位置:网站首页>Select in golang concurrent programming
Select in golang concurrent programming
2022-06-22 03:23:00 【ManNiaoQinFen】
1.select yes Go A control structure in , Be similar to switch sentence , Used to handle asynchronous IO operation .select Will listen case In the sentence channel Read and write operations , When case in channel Read and write operations are non blocking ( Can read and write ) when , Will trigger the corresponding action .
select Medium case The statement must be a channel operation
select Medium default Clause is always runnable .
2. If there are more than one case Can run ,select One of them will be selected randomly and fairly , Others will not be executed .
3. If there is no running case sentence , And there are default sentence , Then it will carry out default The action of .
4. If there is no running case sentence , And there's no default sentence ,select Will block , Until some case Communication is operational
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)
}
}
Running results
chanInt: 10
chanStr: hello
chanStr:
chanStr:
chanStr:
边栏推荐
猜你喜欢

高考后网上查询信息,注意防范没有 SSL证书的网站
![[percona toolkit] series of Pt table checksum and Pt table sync data verification and repair artifacts](/img/1f/89cc2ec9ae6c967791994acac55c04.png)
[percona toolkit] series of Pt table checksum and Pt table sync data verification and repair artifacts
![[nvme2.0b 8] nvme queue arbitration mechanism](/img/35/c5098623c14749711b205ef97c34a7.png)
[nvme2.0b 8] nvme queue arbitration mechanism

图书馆管理系统(PHP期末报告)

Understand virtual memory from the root

AtCoder Regular Contest 142
![[pwn basics]pwntools learning](/img/67/6fb8a9628d5b750b0396f083aaeb91.png)
[pwn basics]pwntools learning
![[nvme2.0b 6] nvme queue model](/img/e9/d29001cebeebe9677b02ffb7c25726.png)
[nvme2.0b 6] nvme queue model

图数据库ONgDB Release v-1.0.2

【NVMe2.0b 5】NVM Subsystem
随机推荐
Force buckle 295 Median data flow
golang并发编程之原子操作详解
[pwn basics]pwntools learning
你是一名技术管理者还是项目管理者?
【NVMe2.0b 6】NVMe 队列模型
[percona toolkit] series of Pt table checksum and Pt table sync data verification and repair artifacts
微信小程序聊天 表情
golang标准库time
Irregular layout of table label
Redis configuration and optimization
[crawler notes 1] environment construction and necessary tools selenium
Unity3d C # generates non repeated random numbers in the interval
【Kubernetes 系列】Kubernetes 是什么?
策略模式
Installing and using mysql8 under Linux (Reprint)
克隆的vmware虚拟主机网卡无法启动解决办法
On map state mapping
Policy mode
A solution to memory leak in server
Fastdfs-5.0.5 installation