Wraps an input channel into a Golang context.Context object.
The returned context will act according to the following rules:
- It is fulfilled as soon as one of the components is fulfilled.
- If it involves an error, the merged object will return the same error.
- The context object has an additional method that returns the component index that was signalled.
import (
"github.com/mxmauro/channelcontext"
)
func main() {
// Create a channel with an integer element
intCh := make(chan int)
// Create the context to wrap the channel
ctx, cancelCtx := channelcontext.New[int](intCh)
defer cancelCtx()
// Send, in background, some value to the channel
go func() {
intCh <- 5
}()
// Wait for the context to be fulfilled
<-ctx.Done()
// We can retrieve the value that fulfilled the context if no error.
if ctx.Err() != nil || ctx.DoneValue() != 5 {
// Signal error
}
// ....
}See LICENSE file for details.