Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chan ¶
type Chan[T any] chan T
Chan is a channel with utility methods.
func (Chan[T]) Recv ¶
Recv will try to receive a value from the channel, same as <-c. If the context is canceled before a value is received, it will return the context error.
func (Chan[T]) RecvTimeout ¶
RecvTimeout will try to receive a value from the channel, same as <-c. If the context is canceled before a value is received or the timeout is reached, it will return the context error.
type ChanIn ¶
type ChanIn[T any] chan<- T
ChanIn is an input channel with utility methods.
type ChanOut ¶
type ChanOut[T any] <-chan T
ChanOut is an output channel with utility methods.
func (ChanOut[T]) Recv ¶
Recv will try to receive a value from the channel, same as <-c. If the context is canceled before a value is received, it will return the context error.
func (ChanOut[T]) RecvTimeout ¶
RecvTimeout will try to receive a value from the channel, same as <-c. If the context is canceled before a value is received or the timeout is reached, it will return the context error.
type Select ¶ added in v0.2.0
type Select[T any] struct { // contains filtered or unexported fields }
Select is a utility for selecting from multiple channels. It is similar to the select statement, but it can handle a dynamic number of channels.
The Select type is not safe for concurrent use. If you need to use it concurrently, you should synchronize access to it.
Example:
ch1 := make(chan int)
ch2 := make(chan int)
s := NewSelect(ch1, ch2)
for s.HasMore() {
selected, err := s.Select(context.Background())
if err != nil {
// context error
break
}
if !selected.Ok {
// channel closed
continue
}
switch selected.Index {
case 1:
fmt.Println("ch1:", selected.Value)
case 2:
fmt.Println("ch2:", selected.Value)
}
}
Example ¶
ctx := context.Background()
ch0 := make(chan int)
ch1 := make(chan int)
ch2 := make(chan int)
go func() {
ch0 <- 1
ch1 <- 2
close(ch0)
time.Sleep(time.Millisecond) // give time for the closed channel to be detected
ch2 <- 3
close(ch2)
time.Sleep(time.Millisecond) // give time for the closed channel to be detected
close(ch1)
}()
s := NewSelect(ch0, ch1, ch2)
for s.HasMore() {
selected, err := s.Select(ctx)
if err != nil {
// context error
break
}
if !selected.Ok {
// channel closed
fmt.Printf("ch%d closed\n", selected.Index)
continue
}
fmt.Printf("ch%d: %v\n", selected.Index, selected.Value)
}
// no more values to read, Select will block until the context is canceled
ctx, cancel := context.WithTimeout(ctx, time.Millisecond)
defer cancel()
_, err := s.Select(ctx)
fmt.Println(err)
Output: ch0: 1 ch1: 2 ch0 closed ch2: 3 ch2 closed ch1 closed context deadline exceeded
func (*Select[T]) ClosedChannels ¶ added in v0.2.0
func (s *Select[T]) ClosedChannels() []<-chan T
ClosedChannels returns the closed channels that were removed from the select case. If no channels are closed, it will return nil.
func (*Select[T]) HasMore ¶ added in v0.2.0
HasMore returns true if there are more channels to select from. If all channels are closed, it will return false.
func (*Select[T]) OpenChannels ¶ added in v0.2.0
func (s *Select[T]) OpenChannels() []<-chan T
OpenChannels returns the open channels that are still part of the select case. If all channels are closed, it will return nil. Note that channels in the returned slice are not necessarily open, they are just the channels that are still part of the select case (potentially open).
func (*Select[T]) Select ¶ added in v0.2.0
Select selects a value from the channels. If a channel is closed, it will be removed from the select case. If the context is canceled, it will return the context error. Note that running Select after HasMore has returned false will always block until the context is canceled, same as a select statement with a single case that receives from ctx.Done().
type Selected ¶ added in v0.2.0
type Selected[T any] struct { // Index is the index of the selected channel. If the context was canceled, // it will be -1. Index int // Value is the value received from the selected channel. If the context was // canceled or the channel was closed, it will be the zero value of T. Value T // Ok is true if the value was received from the channel. If the context was // canceled or the channel was closed, it will be false. Ok bool }
Selected is the result of a select operation.