Documentation
¶
Overview ¶
Package safechan provides safe channel utilities for Go.
It offers panic-free send operations, context-aware channel communication, and channel combinators such as fan-in, fan-out, and broadcast.
Index ¶
- func Broadcast[T any](ch <-chan T, n int) []<-chan T
- func Drain[T any](ch <-chan T) []T
- func DrainCtx[T any](ctx context.Context, ch <-chan T) []T
- func FanIn[T any](channels ...<-chan T) <-chan T
- func FanOut[T any](ch <-chan T, n int) []<-chan T
- func Filter[T any](in <-chan T, pred func(T) bool) <-chan T
- func Map[T, R any](in <-chan T, fn func(T) R) <-chan R
- func Recv[T any](ch <-chan T) (val T, ok bool)
- func RecvCtx[T any](ctx context.Context, ch <-chan T) (val T, ok bool)
- func RecvTimeout[T any](ch <-chan T, d time.Duration) (T, bool)
- func Send[T any](ch chan<- T, val T) (sent bool)
- func SendCtx[T any](ctx context.Context, ch chan<- T, val T) bool
- func SendTimeout[T any](ch chan<- T, val T, d time.Duration) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Broadcast ¶
Broadcast sends each value from the input channel to all n output channels. Every output channel receives every value. All output channels are closed when the input channel is closed.
func Drain ¶ added in v0.2.0
func Drain[T any](ch <-chan T) []T
Drain reads all remaining values from ch without blocking. It returns the collected values immediately when no more values are available in the channel buffer.
func DrainCtx ¶ added in v0.2.0
DrainCtx reads all remaining values from ch, stopping when the context is cancelled or no more values are available in the channel buffer.
func FanIn ¶
func FanIn[T any](channels ...<-chan T) <-chan T
FanIn merges multiple input channels into a single output channel. The output channel is closed when all input channels have been closed. Values are forwarded as they arrive from any input channel.
func FanOut ¶
FanOut distributes values from a single input channel to n output channels in a round-robin fashion. All output channels are closed when the input channel is closed.
func Filter ¶ added in v0.2.0
Filter returns a new channel that only forwards values from in that satisfy the predicate. A background goroutine reads from in and writes matching values to the output channel. The output channel is closed when the input channel is closed.
func Map ¶ added in v0.2.0
func Map[T, R any](in <-chan T, fn func(T) R) <-chan R
Map returns a new channel that transforms each value from in using fn. A background goroutine reads from in, applies fn, and writes the result to the output channel. The output channel is closed when the input channel is closed.
func Recv ¶
Recv receives a value from ch. It returns the value and true if a value was received, or the zero value and false if the channel is closed. This is a thin wrapper around the built-in receive for API consistency.
func RecvCtx ¶
RecvCtx receives a value from ch, respecting context cancellation. It returns the value and true if a value was received, or the zero value and false if the context was done or the channel is closed.
func RecvTimeout ¶ added in v0.2.0
RecvTimeout receives a value from ch with a timeout duration. It returns the value and true if a value was received, or the zero value and false if the deadline expired before a value was available.
func Send ¶
Send sends val on ch without panicking if ch is closed. It returns true if the value was sent successfully, or false if the channel was closed (recovering the panic internally).
Types ¶
This section is empty.