możesz zrobić to tak
for {
select {
case <- chan1:
// ble ble
case <-chan2:
// ble ble 2
}
}
Jeśli nic nie będzie na kanale 1, to będzie sprawdzać kanał 2 i tak w kółko
Ale to mu nie daje priorytetów, które chciał.
https://go.dev/ref/spec#Select_statements
Execution of a "select" statement proceeds in several steps:
- **For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement. **The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated.
-
If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.
- Unless the selected case is the default case, the respective communication operation is executed.
- If the selected case is a RecvStmt with a short variable declaration or an assignment, the left-hand side expressions are evaluated and the received value (or values) are assigned.
- The statement list of the selected case is executed.
Można zrobić tak:
select {
case h := <-high:
fmt.Printf("Job %s is done\n", <-h)
default:
select {
case h := <-high:
fmt.Printf("Job %s is done\n", <-h)
case l := <-low:
fmt.Printf("Job %s is done\n", <-l)
}
}
Swoją drogą, nie bardzo rozumiem, po co ci kanał awake
? Co chcesz osiągnąć? worker
i tak będzie spał, jeżeli select nie ma niczego do wykonania.
Na poniższym przykładzie, widać, że high ma zawsze priorytet, a low jest zagłodzone:
package main
import (
"fmt"
"time"
)
func worker(high, low <-chan chan string, quit <-chan struct{}) {
for {
select {
case <-quit:
return
case h := <-high:
handle(h)
default:
select {
case h := <-high:
handle(h)
case l := <-low:
handle(l)
}
}
}
}
func handle(job <-chan string) {
fmt.Printf("Job %s is done\n", <-job)
}
func patient(p chan<- chan string, quit <-chan struct{}, id string) {
for {
select {
case <-quit:
return
default:
x := make(chan string, 1)
x <- id
p <- x
}
}
}
func main() {
high := make(chan chan string, 10)
low := make(chan chan string, 10)
quit := make(chan struct{})
go patient(low, quit, "low")
go patient(high, quit, "high")
go worker(high, low, quit)
time.Sleep(2 * time.Second)
close(quit)
}