go_channels library

Go-style concurrency for Dart: typed Channels, a faithful select, and structured task scopes with cancellation.

Classes

CancelToken
A cooperative cancellation signal.
Channel<T>
A CSP-style channel that carries values of type T between asynchronous tasks, modelled on Go's channels.
SelectCases<R>
Collects the branches of a select. Register a receive with onReceive, a send with onSend, a non-blocking fallback with onDefault, and a deadline with onTimeout.
TaskScope
A structured-concurrency scope: a group of tasks that all finish before the scope returns, and where one failure cancels the rest (fail-fast).

Functions

select<R>(void build(SelectCases<R> cases)) Future<R>
Waits on several channel operations at once and runs exactly one, modelled on Go's select. Register branches with the SelectCases builder:
waitAll<T>(Iterable<FutureOr<T> Function(CancelToken token)> tasks, {CancelToken? parent}) Future<List<T>>
Runs tasks concurrently under one scope and returns their results in order. Fails fast: the first error cancels the rest and is rethrown.
withTaskScope<R>(FutureOr<R> body(TaskScope scope), {CancelToken? parent}) Future<R>
Runs body in a fresh TaskScope and returns its result once every task spawned inside has finished.
withTimeout<T>(Duration duration, FutureOr<T> task(CancelToken token), {CancelToken? parent}) Future<T>
Runs task with a token that is cancelled after duration.

Exceptions / Errors

CancelledException
Thrown by CancelToken.throwIfCancelled when a token has been cancelled.
ChannelClosedError
Thrown by Channel.receive when the channel is closed and no values are left to drain. Use Channel.receiveOr or Channel.stream if you would rather observe closure without an exception.