channel

package
v0.51.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 0 Imported by: 4 modules, 8 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

channel

Generic combinators over Go channels: Filter, Map, Limit, Reverse, Pipe/PipeWithCancel, Slice/FromSlice, and Closed. Part of rosetta.

What matters here

  • Each combinator spawns a goroutine and owns/closes its OUTPUT channel. Filter, Map, etc. return a new channel and defer close() it from the goroutine they launch. Never close a channel returned by these functions yourself (double-close panics); the combinator closes it when its input drains. Pipe and PipeWithCancel close the output channel you pass in, and Limit closes the done channel you pass in (EmissarySocial BUG-289), so never close those yourself either.
  • The goroutine lives until the INPUT channel closes — so the caller must close the input, or the goroutine leaks. A combinator chained off a channel that never closes is a goroutine leak. When you need to stop early without closing the source, use PipeWithCancel, which selects on a done channel while it waits for input. It does not watch done while it sends, so it still leaks when nothing reads its output.
  • Only Filter and Map buffer their output (size 1); every other channel this package creates is unbuffered. The buffer decouples producer and consumer by one item but does not make the pipeline unbounded — a slow consumer still backpressures the goroutine.
  • Closed is a non-blocking best-effort check, useful in tests and guards; do not use it to make correctness decisions in a concurrent producer, where the state can change immediately after the check. It receives from the channel, so on a channel that holds a value it consumes that value and reports true.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Closed

func Closed[T any](channel <-chan T) bool

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

func Filter[T any](input <-chan T, predicate Predicate[T]) <-chan T

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

func Limit[T any](maximum int, input <-chan T, done chan<- struct{}) <-chan T

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

func Map[Input any, Output any](input <-chan Input, mapper func(Input) Output) <-chan Output

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.

func Reverse

func Reverse[T any](input <-chan T) <-chan T

Reverse reads an entire channel into a stack, then writes it back out in reverse order Since it keeps the entire channel in memory, it should not be used for unbounded data sets

func Slice

func Slice[T any](channel <-chan T) []T

Slice returns a slice of all items in a channel.

Types

type Done added in v0.19.1

type Done chan struct{}

Done is just sugar around a standard channel type that identifies that a process has completed

type Predicate added in v0.19.1

type Predicate[T any] func(T) bool

Predicate is a function that reports whether a value should be included by Filter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL