Documentation ¶
Overview ¶
Package channels provides a number of functions that make working with and composing channels easier.
Most functions in this package take a context argument to facilitate deadlines/cancellation etc.
Index ¶
- func ContextGuard[T any](ctx context.Context, c <-chan T) <-chan T
- func DoAsync[T any](ctx context.Context, f func() (T, error)) <-chan AsyncResult[T]
- func Generate[T any](ctx context.Context, vals ...T) <-chan T
- func Multiplex[T any](ctx context.Context, channels ...<-chan T) <-chan T
- func Receive[T any](ctx context.Context, channel <-chan T) []T
- type AsyncResult
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextGuard ¶
ContextGuard reads values from the given channel into the returned channel, until the given channel is closed or the context is done.
Intended to be used with a for...range loop like so:
for val := range ContextGuard(ctx, inputChannel) {}
Example ¶
channel := Generate(context.Background(), 1, 2, 3) ctx := context.Background() for val := range ContextGuard(ctx, channel) { fmt.Println(val) }
Output: 1 2 3
func DoAsync ¶ added in v0.2.0
func DoAsync[T any]( ctx context.Context, f func() (T, error), ) <-chan AsyncResult[T]
DoAsync can easily run a piece of non-preemptive code in a goroutine, allowing you to wait for the result and see if any errors occurred.
Example ¶
ctx := context.Background() result := DoAsync( ctx, func() (string, error) { // simulate something taking some time to compute <-time.After(time.Millisecond * 3) return "some result", nil }, ) res := <-result fmt.Println(res.Value)
Output: some result
func Generate ¶
Generate sends the variadic values provided to a channel of the same type, closing the channel after all values are sent.
channel := Generate(ctx, 1, 2, 3, 4)
Example ¶
ctx := context.Background() channel := Generate(ctx, 1, 2, 3, 4) received := Receive(ctx, channel) fmt.Println(received)
Output: [1 2 3 4]
func Multiplex ¶
Multiplex will combine multiple channels of the same type into one. Values sent to any of the given channels will be multiplexed to the return channel. Note that order of values received may not be preserved.
c1 := Generate(ctx, 1, 2, 3) c2 := Generate(ctx, 4, 5, 6) multiplexed := Multiplex(ctx, c1, c2)
Example ¶
ctx := context.Background() c1 := Generate(ctx, 1, 2, 3) c2 := Generate(ctx, 4, 5, 6) multiplexed := Multiplex(ctx, c1, c2) received := Receive(ctx, multiplexed) sort.Ints(received) fmt.Println(received)
Output: [1 2 3 4 5 6]
func Receive ¶
Receive will receive all values of the given input channel and append each to a slice, returning it. Receive will block until all values have been read from the given channel.
channel := Generate(ctx, 1, 2, 3, 4) vals := Receive(ctx, channel)
Example ¶
ctx := context.Background() channel := Generate(ctx, 1, 2, 3, 4) vals := Receive(ctx, channel) fmt.Println(vals)
Output: [1 2 3 4]
Types ¶
type AsyncResult ¶ added in v0.2.0
func (AsyncResult[T]) IsError ¶ added in v0.2.0
func (r AsyncResult[T]) IsError() bool