当前位置:网站首页>Go from introduction to actual combat - only any task is required to complete (notes)
Go from introduction to actual combat - only any task is required to complete (notes)
2022-06-27 21:57:00 【Accumulated Ytu】
Use unbuffered 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
}
The number of processes in the above methods will not be changed after the output results , All processes are released , Because there are still many channel In a blocking state
Use cached 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
}
边栏推荐
猜你喜欢
How to design an elegant caching function

Go从入门到实战——协程机制(笔记)

Go从入门到实战——行为的定义和实现(笔记)

GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析

List of language weaknesses --cwe, a website worth learning

Process control task

JVM memory structure when creating objects

Interval DP of Changyou dynamic programming

.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法

Go from introduction to actual combat - package (notes)
随机推荐
GBase 8a OLAP分析函数 cume_dist的使用样例
Tiktok's interest in e-commerce has hit the traffic ceiling?
大厂常用软件测试面试题三(附答案)
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
How to delete "know this picture" on win11 desktop
使用Fiddler模拟弱网测试(2G/3G)
.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
Simulink method for exporting FMU model files
Go从入门到实战——行为的定义和实现(笔记)
The create database of gbase 8A takes a long time to query and is suspected to be stuck
[LeetCode]572. A subtree of another tree
STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP
Magic POI error in reading excel template file
[LeetCode]572. 另一棵树的子树
豆沙绿保护你的双眼
Oracle migration MySQL unique index case insensitive don't be afraid
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
The difference between scrum and Kanban
[LeetCode]30. Concatenate substrings of all words
. Net learning notes (V) -- lambda, LINQ, anonymous class (VaR), extension method