Send and receive through a channel
This deterministic recipe shows the fundamental channel states without relying on scheduler order.
Project tree
text
channels/
└── main.kmSource
ts
import go fmt from "fmt";
function main(): void {
const channel = goChannel[int](2);
channel <- 21;
channel <- 42;
const [first, firstOpen] = <-channel;
const second = <-channel;
closeGoChannel(channel);
const [afterClose, stillOpen] = <-channel;
fmt.Println(first, firstOpen, second, afterClose, stillOpen);
}Run
sh
keika check main.km
keika run main.kmExpected output:
text
21 true 42 0 falseThe buffered sends complete before a receiver starts. The first checked receive returns 21, true; the next receive returns 42. After closing and draining the channel, checked receive returns the int zero value and false.
Boundary behavior
- Sending or closing a closed channel panics as in Go.
- Receiving from a closed, drained channel returns the zero value immediately.
- A nil channel blocks for send/receive; closing it panics.
- Only a bidirectional or send-capable channel can be closed.
selectpreserves Go readiness and nondeterministic selection among ready cases.
Use GoSendChannel<T> and GoReceiveChannel<T> in signatures when direction is part of the API contract.