Search code examples
for-loopgoparallel-processingchannelgoroutine

How to run a method inside for loop in parallel in go?


I have a for loop which iterates over a map of a string as key (keyString) and a slice of type Data (sliceValue) as values. Inside that for loop I have a function process() that takes the sliceValue and keyString and does some operation on it.

I want the process function to be executed in parallel for all slices.

The code that I am mentioning is like this:

for keyString, sliceValue := range mapWithKeyStringAndSliceValue {
     result, err := process(keyString, sliceValue)
     // some other code after this
}

As I mentioned above, the process function should be executed in parallel for all the sliceValues.

I looked at this question to get some idea but it has a bit different operation to do. I am new to channel and go routines and would appreciate any help!


Solution

  • Use sync.WaitGroup and do process inside the loop in go func.

        wg := new(sync.WaitGroup)
        for keyString, sliceValue := range mapWithKeyStringAndSliceValue {
            wg.Add(1)
            // put your sliceValue type instead of interface{}
            go func(keyString string, sliceValue interface{}, wg *sync.WaitGroup) {
                defer wg.Done()
                result, err := process(keyString, sliceValue)
                // some other code after this
            }(keyString, sliceValue, wg)
        }
        
        wg.Wait()