safechan

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 2 Imported by: 0

README

safechan

Safe channel utilities for Go. Provides panic-free send/receive operations, context-aware channel communication, and channel combinators (fan-in, fan-out, broadcast).

Install

go get github.com/philiprehberger/go-safechan

Usage

Safe Send (no panic on closed channel)
import "github.com/philiprehberger/go-safechan"

ch := make(chan int, 1)
ok := safechan.Send(ch, 42) // true

close(ch)
ok = safechan.Send(ch, 1) // false, no panic
Context-aware Send
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

ch := make(chan int)
ok := safechan.SendCtx(ctx, ch, 42) // false if ctx expires or ch is closed
Context-aware Receive
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

val, ok := safechan.RecvCtx(ctx, ch) // returns zero value and false on timeout
Fan-In (merge channels)
merged := safechan.FanIn(ch1, ch2, ch3)
for val := range merged {
    fmt.Println(val)
}
Fan-Out (round-robin distribution)
outputs := safechan.FanOut(input, 3)
// Values from input are distributed round-robin to outputs[0], outputs[1], outputs[2]
Broadcast (send to all)
outputs := safechan.Broadcast(input, 3)
// Every value from input is sent to all 3 output channels

API

Function Description
Send[T](ch, val) Safe send; returns false instead of panicking on closed channel
SendCtx[T](ctx, ch, val) Context-aware send; returns false on cancellation or closed channel
Recv[T](ch) Receive with ok flag
RecvCtx[T](ctx, ch) Context-aware receive; returns zero + false on cancellation
FanIn[T](channels...) Merge multiple channels into one
FanOut[T](ch, n) Distribute values round-robin to n channels
Broadcast[T](ch, n) Send each value to all n channels

License

MIT

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Broadcast

func Broadcast[T any](ch <-chan T, n int) []<-chan T

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 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

func FanOut[T any](ch <-chan T, n int) []<-chan T

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 Recv

func Recv[T any](ch <-chan T) (val T, ok bool)

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

func RecvCtx[T any](ctx context.Context, ch <-chan T) (val T, ok bool)

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 Send

func Send[T any](ch chan<- T, val T) (sent bool)

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).

func SendCtx

func SendCtx[T any](ctx context.Context, ch chan<- T, val T) bool

SendCtx sends val on ch, respecting context cancellation. It returns true if the value was sent successfully, or false if the context was done or the channel was closed.

Types

This section is empty.

Jump to

Keyboard shortcuts

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