当前位置:网站首页>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
}
}
}
边栏推荐
- 2022-2028 global intelligent interactive tablet industry research and trend analysis report
- Research Report on the development trend and Prospect of global and Chinese zinc antimonide market Ⓚ 2022 ~ 2027
- 什么是uid?什么是Auth?什么是验证器?
- Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
- After unplugging the network cable, does the original TCP connection still exist?
- Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
- 【LeetCode 42】501. Mode in binary search tree
- C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
- Global and Chinese market of bipolar generators 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese trisodium bicarbonate operation mode and future development forecast report Ⓢ 2022 ~ 2027
猜你喜欢
地平线 旭日X3 PI (一)首次开机细节
Solve the problem of "Chinese garbled MySQL fields"
Some points needing attention in PMP learning
Codeforces Round #803 (Div. 2)(A-D)
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
What exactly is DAAS data as a service? Don't be misled by other DAAS concepts
After unplugging the network cable, does the original TCP connection still exist?
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
MySQL foundation 02 - installing MySQL in non docker version
Industry depression has the advantages of industry depression
随机推荐
After unplugging the network cable, does the original TCP connection still exist?
《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)
Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
C语言-入门-基础-语法-[运算符,类型转换](六)
Awk from entry to earth (15) awk executes external commands
Awk from getting started to digging in (11) detailed explanation of awk getline function
awk从入门到入土(7)条件语句
2022-2028 research and trend analysis report on the global edible essence industry
Basic discipline formula and unit conversion
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
Reading notes on how to connect the network - hubs, routers and routers (III)
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
MySQL foundation 02 - installing MySQL in non docker version
Trees and graphs (traversal)
2022-2028 global edible probiotic raw material industry research and trend analysis report
Launpad | basic knowledge
《网络是怎么样连接的》读书笔记 - FTTH
Launpad | 基础知识
In depth investigation and Strategic Research Report on China's motion controller Market (2022 Edition)