惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
T
The Exploit Database - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
AWS News Blog
AWS News Blog
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
Google Online Security Blog
Google Online Security Blog
T
Troy Hunt's Blog
I
InfoQ
L
LINUX DO - 热门话题
WordPress大学
WordPress大学
C
Cisco Blogs
G
GRAHAM CLULEY
The Register - Security
The Register - Security
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
H
Hacker News: Front Page
小众软件
小众软件
雷峰网
雷峰网
The Hacker News
The Hacker News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
博客园 - 聂微东
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
The GitHub Blog
The GitHub Blog
腾讯CDC
P
Palo Alto Networks Blog
Scott Helme
Scott Helme

博客园 - 甩掉裤衩凭风吹

Go学习例子(五) Go学习例子(四) Go学习例子(三) Go学习例子(二) Go学习例子(一) 一、Python基础语法 Python爬虫实战一之爬取糗事百科段子 Python爬虫系列:五、正则表达式 Python爬虫系列:四、Cookie的使用 python爬虫系列:三、URLError异常处理 python系列:二、Urllib库的高级用法 python系列:一、Urllib库的基本使用 二十七、mysql如何确保数据不丢失?有几点值得我们借鉴 二十六、聊聊mysql如何实现分布式锁 二十五、sql中where条件在数据库中提取与应用浅析 二十四、如何正确的使用索引? 二十三、mysql索引管理详解 二十二、mysql索引原理详解 二十一、什么是索引?
Go学习例子(六)
甩掉裤衩凭风吹 · 2021-12-01 · via 博客园 - 甩掉裤衩凭风吹

26、Channel Synchronization

我们可以使用通道来同步跨 goroutines 的执行。下面是使用阻塞接收等待 goroutine 完成的示例。在等待多个 goroutine 完成时,您可能更愿意使用WaitGroup

这是我们将在 goroutine 中运行的函数。该通道将用于通知另一个 goroutine 此函数的工作已完成。done

发送一个值以通知我们已完成。done <- true

启动一个工人 goroutine,给它一个通知的通道。done := make(chan bool, 1)

阻止,直到我们收到来自频道上工作人员的通知。 <-done

 

$ go run channel-synchronization.go      
working...done   

27、Channel Directions

使用通道作为函数参数时,可以指定通道是仅发送还是接收值。这种特异性增加了程序的类型安全性。

此函数仅接受用于发送值的通道。尝试在此通道上接收将是一个编译时错误。ping

该函数接受一个通道用于接收 (),另一个通道用于发送 ()。pongpingspongs

 

$ go run channel-directions.go
passed message

28、Select

Go的"选择"可让您等待多个通道操作。将 goroutines 和通道与 select 相结合是 Go 的强大功能。

对于我们的示例,我们将跨两个通道进行选择。

每个通道将在一段时间后收到一个值,以模拟例如在并发 goroutine 中执行的 RPC 操作。

我们将使用同时等待这两个值,并在每个值到达时打印它们。select

我们收到值,然后按预期接收。"one""two"

 

$ go run timeouts.go 
timeout 1
result 2