gpt4 book ai didi

go - 选择单个案例 block ,添加默认值 : unblocks

转载 作者:IT王子 更新时间:2023-10-29 00:36:14 25 4
gpt4 key购买 nike

在用这样的东西测试一些代码时:

// ch := make(chan error)

for {
select {
case <- ch:
println("here")
}
}

我注意到如果我不添加 default 代码块:

for {
select {
case <- ch:
println("here")
default:
}
}

如果需要 block ,那么使用range再好不过了,比如:

for {
for _ = range <- ch {
println("here")
}
}

或者在这种情况下使用 select 而不是 range 有什么区别/优势吗?

最佳答案

1- 当你处理一个 channel 时,可以使用for
考虑这个工作代码(The Go Playground):

package main

import "fmt"

func main() {
ch := make(chan int, 2)
ch <- 1
ch <- 2
close(ch)
for range ch {
}
fmt.Println("Done.")
}

这将清空 channel 。
注意:您应该关闭 channel 或者您应该使用break 语句来完成该循环。


2- 当您处理更多 channel 时,您可以使用 select,像这样 (The Go Playground):

for {
select {
case <-pause:
fmt.Println("pause")
select {
case <-play:
fmt.Println("play")
case <-quit:
wg.Done()
return
}
case <-quit:
wg.Done()
return
default:
work()
}
}

3- 使用 nil 和关闭 channel ( The Go Playground ):

package main

import "fmt"

func main() {
var quit chan struct{} // nil

select {
case <-quit:
fmt.Println("1")
default:
fmt.Println("2") // This runs
}

quit = make(chan struct{}, 1)

select {
case <-quit:
fmt.Println("10")
default:
fmt.Println("20") // This runs
}

quit <- struct{}{} // send

select {
case <-quit:
fmt.Println("100") // This runs
default:
fmt.Println("200")
}

close(quit)

select {
case <-quit:
fmt.Println("1000") // This runs
default:
fmt.Println("2000")
}

select {
case <-quit:
fmt.Println("10000") // This runs
default:
fmt.Println("20000")
}
}

输出:

2
20
100
1000
10000

Select statements

A "select" statement chooses which of a set of possible send orreceive operations will proceed. It looks similar to a "switch"statement but with the cases all referring to communicationoperations.

A case with a RecvStmt may assign the result of a RecvExpr to one ortwo variables, which may be declared using a short variabledeclaration. The RecvExpr must be a (possibly parenthesized) receiveoperation. There can be at most one default case and it may appearanywhere in the list of cases.

“select”语句的执行分几个步骤进行:

For all the cases in the statement, the channel operands of receiveoperations and the channel and right-hand-side expressions of sendstatements are evaluated exactly once, in source order, upon enteringthe "select" statement. The result is a set of channels to receivefrom or send to, and the corresponding values to send. Any sideeffects in that evaluation will occur irrespective of which (if any)communication operation is selected to proceed. Expressions on theleft-hand side of a RecvStmt with a short variable declaration orassignment are not yet evaluated. If one or more of the communicationscan proceed, a single one that can proceed is chosen via a uniformpseudo-random selection. Otherwise, if there is a default case, thatcase is chosen. If there is no default case, the "select" statementblocks until at least one of the communications can proceed. Unlessthe selected case is the default case, the respective communicationoperation is executed. If the selected case is a RecvStmt with a shortvariable declaration or an assignment, the left-hand side expressionsare evaluated and the received value (or values) are assigned. Thestatement list of the selected case is executed. Since communicationon nil channels can never proceed, a select with only nil channels andno default case blocks forever.

关于go - 选择单个案例 block ,添加默认值 : unblocks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39107003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com