mic(s)

Let's Merge Channels

July 06, 2019

I encountered a situation while writing an application for text extraction, where I fan-out to process files based on file extension types, but then needed a way to fan-in the results to a single channel of responses for some consumer.

After doing some open-source research (googling), I discovered go had a convenient way of merging channels, which in this case, was processed data channels produced by each file extension type (struct) that implemented the file processor interface.

func mergeChannels(cs ... <-chan ProcessorInterface) <-chan ProcessorInterface {
	outChan := make(chan ProcessorInterface)
	var wg sync.WaitGroup
	wg.Add(len(cs))

	for _, c := range cs {
		go func(channel <-chan ProcessorInterface) {
			defer wg.Done()
			for val := range channel {
				outChan <- val
			}
		}(c)
	}

	go func() {
		wg.Wait()
		close(outChan)
	}()
	return outChan
}

Michelle

Written by Michelle who enjoys reading, writing software, and petting cats. Twitter Github