Coordinate channels with select
This deterministic example prepares a buffered channel before each select so both the ready and default paths are visible.
Source
import go fmt from "fmt";
function receive(ready: boolean): bstring {
const channel = goChannel[int](1);
if (ready) { channel <- 42; }
select {
case const value = <-channel { return "value:" + fmt.Sprint(value); }
default { return "idle"; }
}
}
function send(full: boolean): string {
const channel = goChannel[int](1);
if (full) { channel <- 1; }
select {
case channel <- 2 { return "sent"; }
default { return "blocked"; }
}
}
function main(): void {
fmt.Println(receive(true), receive(false));
fmt.Println(send(false), send(true));
}Run
keika check select.km
keika run select.kmExpected output:
value:42 idle
sent blockedSelection contract
- A receive case is ready when a value can be received immediately.
- A send case is ready when the send can proceed immediately.
defaultruns only when no communication case is ready.- Without
default, select waits until a case becomes ready. - If several cases are ready, Go chooses one pseudo-randomly.
- Case declarations have case-block scope and do not leak.
Checked receive is also accepted:
select {
case const [value, open] = <-channel {
if (!open) { return; }
consume(value);
}
}Select preserves Go evaluation and channel panic/blocking behavior. It does not add cancellation automatically; select on an explicit context.Context.Done() channel when needed.
v0.4.12 builds require open before using a checked payload whose zero would violate its source contract. A one-value reference binding is nullable; handle it with a null guard, ??, or !. Safe-zero elements keep Go behavior.
Receive assignment targets are evaluated only after selection. All target locations are captured before writes, including field/index/pointer targets and property receivers. For example, a setter cannot change the array slot chosen for a later target:
select {
case [holder.value, flags[index]] = <-channel {
// Both target locations were chosen before holder.value's setter ran.
}
}Unsafe checked payloads require local payload/flag bindings so success can be tracked; receive into locals, verify open, then update other storage.
class User { public name: string = "guest" }
function unsafeReceive(users: GoReceiveChannel<User>): string {
select {
case const [user, open] = <-users {
return user.name
}
}
}Conversions into nullable base classes or interfaces preserve null. See zero values and absence.
See Concurrency.