当前位置:网站首页>About the for range traversal operation in channel in golang
About the for range traversal operation in channel in golang
2022-07-04 09:17:00 【The diligent bird that flies first】
note: channel If you want to use for range Traverse ,channel The closing operation must be carried out first .
Patients with a :
func main(){
wg.Add(2)
write := make(chan int, 5)
read := make(chan int, 5)
go writechan(write)
go readchan(write, read)
// Use for range The reason for traversal is that read channel It's closed later
// So you can use for range Traverse
for val := range read{
fmt.Printf("main by %d \n", val)
}
wg.Wait()
}
func writechan(ch chan int){
for i:=0; i<100; i++{
ch <- i
}
close(ch)
wg.Done()
}
func readchan(write, read chan int){
for val := range write{
fmt.Printf(" The data read out is %d \n", val)
read <- val
}
close(read)
wg.Done()
}
Example 2 :
package main
import "fmt"
// Get data from existing channels
// Method 1 : for Dead cycle
// Method 2 : for range
func main(){
data := make(chan int, 1000)
cal := make(chan int, 2000)
wg.Add(1)
go Putdata(data)
for i:=0; i<4; i++{
wg.Add(1)
go Getdata(data, cal)
}
// Wait for all the present process to end close channel
wg.Wait()
close(cal)
for val := range cal{
fmt.Println(val)
}
}
func Putdata(data chan int){
defer wg.Done()
for i:=1; i<=8000; i++{
data <- i
}
close(data)
}
func Getdata(data, cal chan int){
defer wg.Done()
// Note that although it is 4 Threads to get data , Per thread for range Dissimilarity
// But you can still use for range Of
for num := range data{
flag := true
for i:=2; i<num; i++{
if num % i == 0{
flag = false
break
}
}
if flag{
cal <- num
}
}
}
边栏推荐
- The 14th five year plan and investment risk analysis report of China's hydrogen fluoride industry 2022 ~ 2028
- What is uid? What is auth? What is a verifier?
- 【LeetCode 42】501. Mode in binary search tree
- Explain TCP protocol in detail three handshakes and four waves
- 2022-2028 global elastic strain sensor industry research and trend analysis report
- China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
- swatch
- Awk from entry to earth (8) array
- Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
- Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
猜你喜欢

Solve the problem of "Chinese garbled MySQL fields"

After unplugging the network cable, does the original TCP connection still exist?

2022-2028 global elastic strain sensor industry research and trend analysis report

How to batch change file extensions in win10

2022-2028 research and trend analysis report on the global edible essence industry
](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)

Clion console output Chinese garbled code

AMLOGIC gsensor debugging
![C language - Introduction - Foundation - syntax - [operators, type conversion] (6)](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)

2022-2028 global intelligent interactive tablet industry research and trend analysis report
随机推荐
The old-fashioned synchronized lock optimization will make it clear to you at once!
Flutter tips: various fancy nesting of listview and pageview
Awk from getting started to digging in (9) circular statement
AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
Research Report on the current market situation and development prospects of calcium sulfate whiskers in China (2022 Edition)
Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
Horizon sunrise X3 PI (I) first boot details
If you can quickly generate a dictionary from two lists
Educational Codeforces Round 115 (Rated for Div. 2)
How do microservices aggregate API documents? This wave of show~
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
awk从入门到入土(7)条件语句
Educational Codeforces Round 119 (Rated for Div. 2)
Report on the development trend and prospect trend of high purity zinc antimonide market in the world and China Ⓕ 2022 ~ 2027
Sword finger offer 30 contains the stack of Min function
Opencv environment construction (I)
2022-2028 global probiotics industry research and trend analysis report
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
AMLOGIC gsensor debugging