select
select waits for the first of several blocking calls that can proceed, performs it, and runs the block written for it. Each clause is a head, such as Channel.receive(c), followed by a block.
Details
- A head is one of five calls, written in namespace form:
Channel.receive(c),Channel.send(c, v),Context.wait(ctx),WaitGroup.wait(wg), orTime.sleep(d). A head takes no context; aContext.waitclause stands for it. - The operands of every head evaluate first, in order, once. Then
selectwaits until a head can proceed. When several can, it picks one at random, with no order promised. let x =before a head binds its result in the block: a receive bindsT | null,nullwhen the channel is closed, and a send bindsbool.- An
elseclause, written last, runs when no head can proceed at once, so aselectwithelsenever waits, except that a send head on a closed channel is still chosen and panics. - A
selectis an expression whose value is the chosen block's value.return,break, andcontinuein a block act on the function or loop around theselect.
Basic examples
Wait for a reply, but no longer than a timeout:
let replies: Channel[string] = Channel.new()
go {
Time.sleep(1s)
replies.send("pong")
}
select {
let reply = Channel.receive(replies) { println("got ${reply}") }
Time.sleep(20ms) { println("timed out") }
}timed outWith else, select takes a value only if one is already there, and never waits:
let jobs: Channel[string] = Channel.new(capacity: 1)
jobs.send("build")
for _ in [1, 2] {
select {
let job = Channel.receive(jobs) { println("running ${job}") }
else { println("no job") }
}
}running build
no jobScope
Receive
A receive head binds T | null. It binds null once the channel is closed and drained:
let words: Channel[string] = Channel.new(capacity: 1)
words.send("ant")
words.close()
for _ in [1, 2] {
select {
let word = Channel.receive(words) { println(word ?? "closed") }
}
}ant
closedSend
A send head waits until the channel can take the value. Its binding is a bool, and with else a full channel takes the else branch:
let out: Channel[int] = Channel.new(capacity: 1)
for n in [1, 2] {
select {
let sent = Channel.send(out, n) { println("sent ${n}: ${sent}") }
else { println("full, dropped ${n}") }
}
}sent 1: true
full, dropped 2Contexts
A Context.wait head proceeds when the context is done, by cancellation or deadline:
let ctx = Context.new()
let jobs: Channel[int] = Channel.new()
go { ctx.cancel() }
select {
Context.wait(ctx) { println("stopped: ${ctx.reason() ?? ""}") }
let job = Channel.receive(jobs) { println("job ${job}") }
}stopped: cancelledWait groups
A WaitGroup.wait head proceeds when the group's counter is zero, so a group of goroutines can race a deadline:
let wg = WaitGroup.new()
let squares: Channel[int] = Channel.new(capacity: 3)
for n in [1, 2, 3] {
wg.add(1)
go {
defer { wg.done() }
squares.send(n * n)
}
}
select {
WaitGroup.wait(wg) { println("all ${squares.length()} done") }
Time.sleep(5s) { println("too slow") }
}all 3 doneAs an expression
The value of select is the value of the block it runs. Without an expected type, it is the union of the blocks' types:
let counts: Channel[int] = Channel.new(capacity: 1)
counts.send(5)
let n = select {
let count = Channel.receive(counts) { count ?? 0 }
else { "none" }
}
println(n)5With an expected type, each block is checked against it. A union of records names each outcome:
type Line = { text: string }
type Closed = {}
type Timeout = {}
let lines: Channel[string] = Channel.new(capacity: 1)
lines.send("hello")
let event: Line | Closed | Timeout = select {
let line = Channel.receive(lines) { if line == null { Closed{} } else { Line{ text: line } } }
Time.sleep(1s) { Timeout{} }
}
println(event)Line{ text: "hello" }Leaving a loop or function
A block belongs to the body around the select, so its break leaves the loop and its return leaves the function:
func firstLong(words: Channel[string]): string | null {
while true {
select {
let word = Channel.receive(words) {
if word == null { break }
if word.length() > 3 { return word }
}
}
}
null
}
let words: Channel[string] = Channel.new(capacity: 3)
words.send("ant")
words.send("beetle")
words.close()
println(firstLong(words))beetleApplications
Merge two channels into one stream, and stop when both are closed. A closed channel is always ready, so the loop drops it from the select by replacing it with a channel that never delivers:
func merge(a: Channel[int], b: Channel[int]): Array[int] {
let silent: Channel[int] = Channel.new()
var left = a
var right = b
var open = 2
let all: Array[int] = []
while open > 0 {
select {
let x = Channel.receive(left) {
if x == null {
left = silent
open -= 1
} else {
all.push(x)
}
}
let y = Channel.receive(right) {
if y == null {
right = silent
open -= 1
} else {
all.push(y)
}
}
}
}
all.sort()
}
let odds: Channel[int] = Channel.new()
let evens: Channel[int] = Channel.new()
go {
defer { odds.close() }
for n in [1, 3, 5] { odds.send(n) }
}
go {
defer { evens.close() }
for n in [2, 4] { evens.send(n) }
}
println(merge(odds, evens))[1, 2, 3, 4, 5]A worker that handles jobs until its context is cancelled:
func work(jobs: Channel[string], done: Channel[string], ctx: Context) {
while true {
select {
Context.wait(ctx) { return }
let job = Channel.receive(jobs) {
if job == null { return }
done.send("done ${job}")
}
}
}
}
let ctx = Context.new()
let jobs: Channel[string] = Channel.new()
let done: Channel[string] = Channel.new()
go { work(jobs, done, ctx) }
for job in ["lint", "test"] {
jobs.send(job)
println(done.receive() ?? "")
}
ctx.cancel()done lint
done testProperties and relations
A select with one receive head and no else waits the way Channel.receive does and gets the same value:
let c: Channel[int] = Channel.new(capacity: 2)
c.send(7)
c.send(7)
let viaSelect = select {
let v = Channel.receive(c) { v }
}
assert(viaSelect == c.receive())Possible issues
A head is written only in namespace form. The dot form is the same call elsewhere, but not a head:
let c: Channel[int] = Channel.new(capacity: 1)
select {
c.receive() { println("got") }
// error: a select clause's head must be a call of Channel.receive, Channel.send, Context.wait, WaitGroup.wait or Time.sleep
}A head takes no context. Write the context as a clause of its own:
let c: Channel[int] = Channel.new(capacity: 1)
let ctx = Context.new()
select {
Channel.receive(c, ctx) { println("got") }
// error: a select clause's head takes no context: the context is a clause of its own, Context.wait(ctx) { ... }
}A receive binds T | null, not T. Handle the null of a closed channel:
let c: Channel[int] = Channel.new(capacity: 1)
let n: int = select {
let v = Channel.receive(c) { v } // error: cannot use int | null where int is expected
else { 0 }
}Context.wait, WaitGroup.wait, and Time.sleep return void, so their heads bind nothing:
let ctx = Context.new()
select {
let x = Context.wait(ctx) { println("done") }
// error: Context.wait returns void, so there is nothing to bind to x
}The block starts on the head's line. A { on the next line is a syntax error:
let c: Channel[int] = Channel.new(capacity: 1)
select {
let v = Channel.receive(c) // error: expected '{' after the select clause header, got newline
{ println(v) }
}A send on a closed channel is always ready, so select picks it over else and panics:
let c: Channel[int] = Channel.new(capacity: 1)
c.close()
select { // panic: select: a send clause was chosen on a closed channel
Channel.send(c, 1) { println("sent") }
else { println("closed") }
}A closed channel's receive is always ready. A loop that keeps it in its select and does not leave on null spins, picking the closed channel again and again. Leave the loop on null, as in Leaving a loop or function, or swap the channel out, as in Applications.
See also
- go: start the goroutines a
selectwaits on - break, continue, and return in a
selectblock - Channel.receive and Channel.send
- Channel
- Context.wait
- WaitGroup.wait
- Time.sleep
