Documentation
¶
Overview ¶
Package channels provides utilities for working with Go channels, including channel creation with flexible sizing and safe channel closing.
Index ¶
- func CloseChannelIgnorePanic[T any](ch chan<- T)
- func Create[T any](size int) (chan<- T, <-chan T, func() int)
- func InfiniteChan[A any]() (chan<- A, <-chan A, func() int)
- func SendCatchPanic[T any](ch chan<- T, msg T) (err error)
- func SendContextCatchPanic[T any](ctx context.Context, channel chan<- T, msg T) (err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseChannelIgnorePanic ¶
func CloseChannelIgnorePanic[T any](ch chan<- T)
CloseChannelIgnorePanic closes a channel like normal. However, if the channel has already been closed, it will suppress the resulting panic.
func Create ¶
Create creates a channel with the specified size and returns a send-only channel, a receive-only channel, and a function to get the current queue length.
The size parameter determines the channel type:
- size < 0: creates an infinite buffering channel (via InfiniteChan)
- size == 0: creates an unbuffered channel
- size > 0: creates a buffered channel with the specified capacity
Returns:
- chan<- T: send-only channel for writing values
- <-chan T: receive-only channel for reading values
- func() int: function that returns the current number of items in the channel
func InfiniteChan ¶
InfiniteChan creates a channel with infinite buffering. It returns a send-only channel and a receive-only channel. The send-only channel can be used to send values without blocking. The receive-only channel can be used to receive values in the order they were sent.
Note: Use with caution as it can lead to high memory usage if the sender outpaces the receiver. It's recommended to monitor the size of the internal queue if used in a long-running process.
func SendCatchPanic ¶
SendCatchPanic sends a message to a channel and recovers from any panic that occurs. This is useful when sending to a channel that might be closed, avoiding a panic.
Returns nil if the send succeeds or if ch is nil. Returns an error if a panic occurs during the send (e.g., sending on a closed channel).
func SendContextCatchPanic ¶
SendContextCatchPanic sends a message to a channel with context support and recovers from any panic. It respects context cancellation and will return ctx.Err() if the context is canceled before the send.
If ctx is nil, it falls back to SendCatchPanic behavior. Returns nil if the send succeeds or if channel is nil. Returns ctx.Err() if the context is canceled. Returns an error if a panic occurs during the send (e.g., sending on a closed channel).
Types ¶
This section is empty.