Documentation
¶
Overview ¶
Package channel provides generic combinators for working with Go channels: Filter, Map, Limit, Reverse, Pipe, and conversions to and from slices.
Every combinator spawns a goroutine that closes its output when it finishes, usually when its input drains. Most return a new output channel; Pipe and PipeWithCancel write to, and close, the one they are given. Callers must never close a channel this package closes, and must close the input they pass in, or the goroutine outlives them. PipeWithCancel takes a done channel to stop early, while Limit closes the done channel it is given when it stops.
Filter and Map buffer one item, which decouples producer from consumer by a single item; every other channel it creates is unbuffered. A slow consumer still applies backpressure all the way up the chain.
Index ¶
- func Closed[T any](channel <-chan T) bool
- func Filter[T any](input <-chan T, predicate Predicate[T]) <-chan T
- func FromSlice[T any](slice []T) <-chan T
- func Limit[T any](maximum int, input <-chan T, done chan<- struct{}) <-chan T
- func Map[Input any, Output any](input <-chan Input, mapper func(Input) Output) <-chan Output
- func Pipe[T any](input <-chan T, output chan<- T)
- func PipeWithCancel[T any](input <-chan T, output chan<- T, done <-chan struct{})
- func Reverse[T any](input <-chan T) <-chan T
- func Slice[T any](channel <-chan T) []T
- type Done
- type Predicate
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Closed ¶
Closed returns TRUE if a receive from the channel succeeds at once: the channel is closed, or it holds a value, which this call consumes and discards. It returns FALSE otherwise.
func Filter ¶ added in v0.19.1
Filter returns a channel that contains only the items that pass the predicate function.
func FromSlice ¶ added in v0.19.1
func FromSlice[T any](slice []T) <-chan T
FromSlice posts every item from a slice to a channel, and then closes the channel.
func Limit ¶
Limit returns a channel that will receive at most the specified number of items from the input channel. When it stops, at the maximum or when the input closes first, Limit closes the "done" channel, to communicate to other goroutines that they can stop sending items.
func Map ¶ added in v0.19.0
Map applies a mapping function to every value in the input channel, passing the mapped values out to the output channel.
func Pipe ¶
func Pipe[T any](input <-chan T, output chan<- T)
Pipe reads the contents of one channel directly into another channel.
func PipeWithCancel ¶
func PipeWithCancel[T any](input <-chan T, output chan<- T, done <-chan struct{})
PipeWithCancel reads the contents of one channel directly into another channel. If the "done" channel is closed, the pipe will stop.