Skip to content

mxmauro/channelcontext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

channelcontext

Wraps an input channel into a Golang context.Context object.

Behavior

The returned context will act according to the following rules:

  1. It is fulfilled as soon as one of the components is fulfilled.
  2. If it involves an error, the merged object will return the same error.
  3. The context object has an additional method that returns the component index that was signalled.

Example

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
    }

    // ....
}

LICENSE

See LICENSE file for details.

About

Wraps an input channel into a Golang context.Context object.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages