Documentation
¶
Overview ¶
Utils for safely doing chan operations that respect context.Context.
Use Send or Receive to send/receive on a channel, or IterReceive to range over a channel.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrContextCanceled = errors.New("context canceled") ErrChanClosed = errors.New("channel closed") )
Functions ¶
func IterReceive ¶
Yields all values received from a channel, and stops when either the context is cancelled, or the channel is empty and closed.
It is a safer alternative to:
for val := range ch { /* ... */ }
For context-sensitive operations.
func Receive ¶
Try to receive a value from a channel.
Returns one of the following:
- val, nil: if the value was received
- *new(T), ErrContextCanceled: if the context is canceled
- *new(T), ErrChanClosed: if there are no more values, and the channel is closed
In the case of ErrContextCanceled, the original ctx.Err() is preserved via errors.Join. Use errors.Is to detect it if necessary.
func Send ¶
Try to send a value over a channel. Returns ErrContextCanceled if the context is cancelled.
In the case of ErrContextCanceled, the original ctx.Err() is preserved via errors.Join. Use errors.Is to detect it if necessary.
func TryReceive ¶ added in v1.1.0
Retrieve a value from the channel if there is one ready.
Types ¶
This section is empty.