Sobes.tech
Junior — Senior

Merging multiple streams into a single channel

livecode

Task condition

It is necessary to write a function that collects data from an arbitrary number of input channels and outputs them into a single resulting channel. Each of the input channels can be filled by separate goroutines, and the task of the function is to correctly merge all incoming values and close the output channel only after all sources have finished.

func fanIn(channels ...<-chan int) <-chan int {
    out
    // implementation of merging
    return out
}

The function should ensure safe access to the shared data stream and terminate correctly when all input channels are closed.