当前位置:网站首页>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
}
边栏推荐
- 空指针异常
- oss上传调用的是哪个方法
- How to do a good job of gateway high availability protection in the big promotion scenario
- 教程|fNIRS数据处理工具包Homer2下载与安装
- 让马化腾失望了!Web3.0,毫无希望
- Oracle的CTAS能不能将约束等属性带到新表?
- CORBA 架构体系指南(通用对象请求代理体系架构)
- 小王的面试训练任务
- Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
- SQL必需掌握的100个重要知识点:使用函数处理数据
猜你喜欢
Unity3d button adapts the size according to the text content
CORBA 架构体系指南(通用对象请求代理体系架构)
100 important knowledge points that SQL must master: using functions to process data
创建对象时JVM内存结构
展现强劲产品综合实力 ,2022 款林肯飞行家Aviator西南首秀
Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China
富文本 考试 填空题
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
麒麟V10安装字体
Can Oracle's CTAs bring constraints and other attributes to the new table?
随机推荐
GoLand permanently activated
Cortical traceability analysis of ERP
Flexible IP network test tool -- x-launch
快递e栈——数组篇小型项目
如何将队列里面的内容没秒钟执行一次优先级
squid代理服务器
请教CMS小程序首页的幻灯片在哪里设置?
VMware vSphere ESXi 7.0安装教程
使用storcli工具配置RAID,收藏这一篇就够了
GoLand永久激活
MySQL usage notes 1
Codeforces Round #719 (Div. 3)
Educational Codeforces Round 108 (Rated for Div. 2)
数组作业题
关于异常处理的知识整理
Serveur mandataire SQUID
系统自带的karsonzhang/fastadmin-addons报错
麒麟V10安装字体
SQL Server for循环用法
大促场景下,如何做好网关高可用防护