2

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!

Panda
  • 499
  • 11
  • 22
  • 1
    IF you need `result` and `err` in sync, there's no advantage (actually a disadvantage) to execute `process(...)` async – Felix Jun 07 '21 at 12:52
  • 2
    See related: [How to collect values from N goroutines executed in a specific order?](https://stackoverflow.com/questions/37856337/how-to-collect-values-from-n-goroutines-executed-in-a-specific-order/37857863#37857863) – icza Jun 07 '21 at 12:54
  • 1
    If you don't need to collect the results, just put the the loop body into a function (named or anonymous) and launch it as a goroutine. Use a `WaitGroup` should you need to wait all a launched goroutines to complete. – icza Jun 07 '21 at 12:57

1 Answers1

3

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()
jub0bs
  • 60,866
  • 25
  • 183
  • 186
nipuna
  • 3,697
  • 11
  • 24
  • What if I want to return in case of error, like `if err != nil` and I want to return this err or `if result == nil` then I want the loop to continue – Panda Jun 07 '21 at 13:04
  • 1
    Continue loop is useless when you open go routines in every iterations. If you need to handle error in every iteration, you need to do channel operation or do this in sequence. – nipuna Jun 07 '21 at 13:08