I have a method that queries an API to obtain data for different date ranges with or without a filter.
func getTopData(country string, startDate time.Time, endDate time.Time, filterID uint) (result map[string][10]topResult) {
response := getRequest(fmt.Sprintf("%s/top/%s/%s-%s/filterid:%d/10",
cfg.API.URI,
country,
startDate.Format("20060102"),
endDate.Format("20060102"),
filterID))
json.Unmarshal(response, &result)
return
}
Each call takes about one minute to finish and I need to call the method six times with different parameters (for each of the last three months without filter and each of the last three months with filter).
So now I have something like this:
var topData [6]map[string][10]topResult
topData[0] = getTopData(country, firstMonthStart, firstMonthEnd, 0)
topData[1] = getTopData(country, secondMonthStart, secondMonthEnd, 0)
topData[2] = getTopData(country, thirdMonthStart, thirdMonthEnd, 0)
topData[3] = getTopData(country, firstMonthStart, firstMonthEnd, filterID)
topData[4] = getTopData(country, secondMonthStart, secondMonthEnd, filterID)
topData[5] = getTopData(country, thirdMonthStart, thirdMonthEnd, filterID)
With this, the data is filled one after another. How would I go to make the calls in parallel while maintaining the order of the results in the topData
array (so that the data at index 0 is for the first month without filter, data at index 4 is the second month with filter, etc.)?
I was thinking about using a WaitGroup, but I am not really sure how to handle the actual result data. :(
So, first thing would be adding wg *sync.WaitGroup
as parameter to the getTopData
function. Inside I do a wg.Add(1)
and defer wg.Done()
. Outside the function, I do var wg sync.WaitGroup
and pass &wg
to getTopData
, do my six calls and then wg.Wait()
. But how do I assign the results to topData[0]
, topData[1]
, etc.?
I went ahead and did the following changes:
getTopData
has a parameter result *map[string][30]topResult
getTopData
getTopData
six times and pass &topData[0]
, &topData[1]
, etc. as parameter