I have written goroutine inside for loop as follow. What I want is to execute the goroutine for each item in the listofDevices list concurrently but this logic is running goroutine for only one item in the listofDevices list.
for _, ip := range listOfDevices {
inChan <- types.NewNotification(time.Now(), "/cloudtracer/status", nil,
&map[key.Key]interface{}{
key.New(ip): types.Pointer{Pointer: "/cloudtracer/status/" + ip},
})
pingOneMachine := probe.NewPing(ip, 2*time.Second, inChan)
// For now stopping the probe after few minutes. This will change later
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
wg.Add(len(listOfDevices))
go func() {
defer wg.Done()
pingOneMachine.Run(ctx)
}()
}
wg.Wait()
Can anyone help me in fixing this out? thanks in advance.