Documentation ¶
Index ¶
- func AsReceiver[T any](a []chan T) []<-chan T
- func AsSender[T any](a []chan T) []chan<- T
- func ChanToSlice[T any](ctx context.Context, ch <-chan T) ([]T, error)
- func ChanToSliceN[T any](ctx context.Context, ch <-chan T, n int) ([]T, error)
- func CombineIntoN[T any](ctx context.Context, dst chan<- T, src ...<-chan T) (int, error)
- func CombineN[T any](ctx context.Context, src ...<-chan T) <-chan T
- func ForkJoin[T, R any](ctx context.Context, a []T, f func(context.Context, T) (R, error)) (r []R, e []error)
- func Range[T constraints.Integer](n int) chan T
- func SliceToChan[T any](a []T) <-chan T
- func Tee[T any](ctx context.Context, src <-chan T) (a, b <-chan T)
- func TeeN[T any](ctx context.Context, src <-chan T, n int) (copies []<-chan T)
- func TeeTo[T any](ctx context.Context, src <-chan T, L, R chan<- T) error
- func TeeToN[T any](ctx context.Context, src <-chan T, dst []chan<- T) (n int, err error)
- type Result
- type Results
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsReceiver ¶
func AsReceiver[T any](a []chan T) []<-chan T
AsReceiver downcasts a slice of bidirectional channels to receive-only channels. The returned slice points to the same underlying array: modify it at your peril!
func AsSender ¶
func AsSender[T any](a []chan T) []chan<- T
AsSender downcasts a slice of bidirectional channels to send-only channels. The returned slice points to the same underlying array: modify it at your peril!
func ChanToSlice ¶
ChanToSlice collects the results of ch into a slice, returning when the channel is closed or the context is done, whichever comes first. Termination: this terminates if the context does.
func ChanToSliceN ¶
ChanToSliceN collects at most N elements of ch into a slice, returning under the following conditions: - the channel is closed - the context is done - N elements have been collected
func CombineIntoN ¶
CombineIntoN feeds N input channels into a single output channel.
func ForkJoin ¶
func ForkJoin[T, R any](ctx context.Context, a []T, f func(context.Context, T) (R, error)) (r []R, e []error)
ForkJoin executes a function 'f' in parallel across a slice T. The following guarantees hold:
- len(a) == len(r) == len(e)
- r[i], err[i] = f(a[i])
`f` must be responsible for respecting the context's cancellation. ForkJoin terminates if and only if f(ctx, a[i]) does.
func Range ¶
func Range[T constraints.Integer](n int) chan T
func SliceToChan ¶
func SliceToChan[T any](a []T) <-chan T
SliceToChan converts a slice []T to a buffered channel of the same length.
func Tee ¶
Tee immediately returns two channels, each of which outputs the identical input from src. That is, for every read from src, Tee writes a shallow copy to A and to B. Be careful with pointers. See TeeN for more than two copies.
func TeeN ¶
TeeN is the generalized form of Tee. It returns N channels, each of which outputs the identical input from src. That is, for every read from src, Tee writes a shallow copy to A and to B. Be careful with pointers. See Tee for just two output channels.
func TeeTo ¶
TeeTo is as Tee, but pipes to the provided channels instead of making it's own. It does NOT close the channel. Termination: this function terminates when L and R are drained or when the context is cancelled.
func TeeToN ¶
TeeToN is as TeeN, but pipes to the provided channels instead of making it's own. Safety: this is considered to take ownership of the *slice* dst, which is not safe for use afterwards. For every read from src, TeeTooN writes a shallow copy to each channel in dst.
Terminates when: - the context is cancelled: - src has been closed and every entry has been written to dst.
Safety:
Replacing dst[i] (with nil or with another channel) after a call to TeeToN is undefined behavior. Failing to drain any given chaanel may block the others. Be careful with pointers.
Types ¶
type Results ¶
Results is a read-only channel of the results of a mapping operation.
func StreamMap ¶
func StreamMap[T, R any](ctx context.Context, in chan T, f func(context.Context, T) (R, error)) Results[T, R]
StreamMap executes "f" in parallel across the items piped to "in", creating a ResultStream. It will attempt to terminate early if the context is done, but "f" is responsible for respecting the context's cancellation, and the consumer(s) of the ResultStream must either consume the result entirely or drop it out of scope.
func (Results[T, R]) CollectN ¶
CollectN collects at most N elements from the result stream. Termination:
func (Results[T, R]) CollectNCtx ¶
CollectNCtx elements from the ResultStream until one of these happens:
- The ResultStream is closed.
- N elements have been obtained.
- The context has been cancelled.
Note that this allocates a slice of [R] of capacity N.