How to Implement the Or-Channel Pattern in Go

Implement the Or-Channel pattern in Go by using a select statement within a goroutine to return the first value received from multiple input channels.

The Or-Channel pattern in Go is implemented by spawning a goroutine for each potential data source and using a select statement to receive from the first channel that sends a value.

func or(c1, c2 <-chan T) <-chan T {
    out := make(chan T)
    go func() {
        select {
        case v := <-c1:
            out <- v
        case v := <-c2:
            out <- v
        }
    }()
    return out
}

This pattern extends to multiple channels by adding more case statements inside the select block.