It is likely that in an application there are multiple threads each possibly sending and receiving on multiple channels.
alt
statement selects statements to execute based on the readiness of channels. The alt
statement is like a case
statement for channels.
The qualifiers in the alt
statement must be expressions containing the channel communication operator, <-
.
If there is no channel ready to send or receive, and a default qualifer, *, is present, then the default is selected. If the default qualifier is not present, then the statement blocks until a channel is ready.
Each qualifer and the statements that follow up to the next qualifier form a block. Like case
statements, each block breaks by default; no explicit break
statement is needed.
Its syntax is similar to the case
statement. For example:
outchan := chan of string; inchan := chan of int; i := int; alt { i = <- inchan => ... outchan <- = "sent" => ... }In this example, the selection is based on two channels,
inchan
and outchan
. If inchan
is ready to receive, it is selected. If outchan
is ready to send, it is selected. If both are ready at the same time, it is indeterministic which one will be selected. The order of the qualifiers does not affect the selection.