I am writing a code to upload users timesheet to AWS S3 bucket. There are around 150 users, so I am trying to make the code to run concurrently for all the users using waitgroup. Below is my main() which is working without errors, but it is uploading incorrect data. ie. uploading the same data for multiple users in a span. I believe the same variables are getting updated for all the users. Can anyone point me how to solve this issue?
func main() {
// Get list of users
agentsList := getAgentsList(listUsersURL, 1)
fmt.Println("Total Users:", len(agentsList))
// Get call details of all the users
var waitgroup sync.WaitGroup
waitgroup.Add(len(agentsList))
for count, user := range agentsList {
go func() {
fmt.Println("\nAdding User in waitgroup", count)
FirstCall, LastCall := getFirstAndLastCall(user.UserID)
// Upload timesheet to S3 bucket
postTimeSheetToS3(timesheetDate, user.UserID, FirstCall, LastCall)
}
fmt.Println("\nRemoving User from waitgroup", count)
waitgroup.Done()
}()
}
waitgroup.Wait()
}