Back to Lo

Core Fanin

docs/data/core-fanin.md

1.53.0542 B
Original Source

FanIn merges multiple upstream channels into a single downstream channel, reading from all input channels and forwarding their values to one output channel. The channelBufferCap parameter sets the buffer size of the output channel.

go
ch1 := make(chan int, 2)
ch2 := make(chan int, 2)
ch1 <- 1
ch1 <- 2
ch2 <- 3
ch2 <- 4
close(ch1)
close(ch2)

merged := lo.FanIn(10, ch1, ch2)
var result []int
for item := range merged {
    result = append(result, item)
}
// result: []int{1, 2, 3, 4} (order may vary as goroutine scheduling differs)