channel

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 1 Imported by: 8

README

channel

Generic combinators over Go channels: Filter, Map, Limit, Beep, Pipe/PipeWithCancel, 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.
  • 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 and returns when signaled.
  • Output channels are buffered (size 1). This 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.

Documentation

Overview

Package channel provides generic combinators for working with Go channels: Filter, Map, Limit, Reverse, Beep, Pipe, and conversions to and from slices.

Every combinator spawns a goroutine, returns a new output channel, and owns that channel's lifetime — it closes the output when its input drains. Callers must never close a channel returned from this package, and must close the input they pass in, or the goroutine outlives them. PipeWithCancel and Limit take a done channel for the cases where stopping early is the only way out.

Output channels carry a buffer of one, which decouples producer from consumer by a single item without making the pipeline unbounded. 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 Beep added in v0.19.0

func Beep[T any](in <-chan T) <-chan T

Beep is a simple channel that dumps all of its input to the console. It can be used to debug a channel's contents without disrupting the flow of data.

func Closed

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

Closed returns TRUE if this channel is closed, and FALSE otherwise. It does not read from the channel, and is just a simple wrapper around a select statement.

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 the maximum is reached, Limit will close 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