当前位置:网站首页>Go从入门到实战——仅需任意任务完成(笔记)
Go从入门到实战——仅需任意任务完成(笔记)
2022-06-27 19:23:00 【积淀 ytu】
使用无缓冲的channel
func runTask(id int) string {
time.Sleep(10 * time.Millisecond)
return fmt.Sprintf("The result is from %d", id)
}
func FirstResponse() string {
numOfRunner := 10
ch := make(chan string)
for i := 0; i < numOfRunner; i++ {
go func(i int) {
ret := runTask(i)
ch <- ret
}(i)
}
return <-ch
}
以上方法协程数量不会因为输出结果之后,所有协程都释放,因为还有很多channel处于阻塞状态
使用有缓存的channel
func runTask(id int) string {
time.Sleep(10 * time.Millisecond)
return fmt.Sprintf("The result is from %d", id)
}
func FirstResponse() string {
numOfRunner := 10
ch := make(chan string,numOfRunner)
for i := 0; i < numOfRunner; i++ {
go func(i int) {
ret := runTask(i)
ch <- ret
}(i)
}
return <-ch
}
边栏推荐
- VMware vSphere ESXi 7.0安装教程
- 本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
- Educational Codeforces Round 108 (Rated for Div. 2)
- 请教CMS小程序首页的幻灯片在哪里设置?
- Codeforces Round #717 (Div. 2)
- Tutorial | fNIRS data processing toolkit homer2 download and installation
- SQL Server for循环用法
- SQL必需掌握的100个重要知识点:用通配符进行过滤
- Shell command used in actual work - sed
- Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
猜你喜欢

大促场景下,如何做好网关高可用防护

SQL必需掌握的100个重要知识点:排序检索数据

After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"

Wechat applet based service management system for college party members' Home System applet graduation design, Party members, activists, learning, punch in, forum

Squid proxy server

Use the storcli tool to configure raid. Just collect this article

Let Ma Huateng down! Web3.0, hopeless

富文本 考试 填空题

Oracle的CTAS能不能将约束等属性带到新表?

使用storcli工具配置RAID,收藏这一篇就够了
随机推荐
Golang 使用正则来匹配出子字符串函数
Tutorial | fNIRS data processing toolkit homer2 download and installation
小王的面试训练任务
Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
Cortical traceability analysis of ERP
Flood fighting and disaster relief, overcoming difficulties, and City United premium products rushed to the aid of Yingde to donate loving materials
Can Oracle's CTAs bring constraints and other attributes to the new table?
BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!
Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
MySQL Express - day 1 - basic introduction
Codeforces Round #717 (Div. 2)
2021全球独角兽榜发布:中国301家独角兽企业全名单来了!
OpenSSL 编程 一:基本概念
开启生态新姿势 | 使用 WrodPress 远程附件存储到 COS
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
大促场景下,如何做好网关高可用防护
Share an experience of self positioning + problem solving
After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"
创建对象时JVM内存结构
快递e栈——数组篇小型项目