当前位置:网站首页>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
}
}
}
边栏推荐
- China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
- 2022-2028 global tensile strain sensor industry research and trend analysis report
- 什么是uid?什么是Auth?什么是验证器?
- Codeforces Round #803 (Div. 2)(A-D)
- Talk about single case mode
- A subclass must use the super keyword to call the methods of its parent class
- 2022-2028 global probiotics industry research and trend analysis report
- Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
- 2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
- Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
猜你喜欢
HMS core helps baby bus show high-quality children's digital content to global developers
保姆级JDEC增删改查练习
【LeetCode 42】501. Mode in binary search tree
2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
C語言-入門-基礎-語法-[運算符,類型轉換](六)
Ehrlich sieve + Euler sieve + interval sieve
2022-2028 global tensile strain sensor industry research and trend analysis report
C语言-入门-基础-语法-[运算符,类型转换](六)
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
HMS core helps baby bus show high-quality children's digital content to global developers
随机推荐
Development trend and market demand analysis report of high purity tin chloride in the world and China Ⓔ 2022 ~ 2027
2022-2028 global special starch industry research and trend analysis report
Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
What is uid? What is auth? What is a verifier?
Flutter 小技巧之 ListView 和 PageView 的各種花式嵌套
Explain TCP protocol in detail three handshakes and four waves
Awk from entry to earth (12) awk can also write scripts to replace the shell
After unplugging the network cable, does the original TCP connection still exist?
Research Report on the development trend and Prospect of global and Chinese zinc antimonide market Ⓚ 2022 ~ 2027
Awk from getting started to digging in (9) circular statement
2022-2028 global elastic strain sensor industry research and trend analysis report
随机事件的关系与运算
PMP registration process and precautions
Dede plug-in (multi-function integration)
《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
2022-2028 global protein confectionery industry research and trend analysis report
Markdown syntax
Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
Use Alibaba cloud NPM image acceleration