Hi, I run a method from the main function, in this method, depending on the result, the goroutines are processed, how can I wait for their execution and display the result?
I tried this, but for some reason the result is not displayed
func main() {
wgCmd := new(sync.WaitGroup)
var resxhws *sysinfo.HWXEquipData
var xhws = []string{"airportxequip", "appsxequip", "audioxequip", "powerxequip"}
resMoreXHW, resErrsInfo := resxhws.GetMoreHWXInfo(xhws, wgCmd)
wgCmd.Wait()
fmt.Println(resMoreXHW, resErrsInfo)
}
func (morexhw *HWXEquipData) GetMoreHWXInfo(xhws []string, wg *sync.WaitGroup) (*HWXEquipData, []*ResInfoError) {
resErrs := []*ResInfoError{}
conf, err := config.NewCmd("config/cmd.yml")
if err != nil {
log.Fatalf("can't read the config: %s", err)
}
var resAir *AirPortData
var resApp *AppsData
var resAudio *AudioData
var resPower *PowerData
for _, xhw := range xhws {
wg.Add(1)
if xhw == "airportxequip" {
go func() {
defer wg.Done()
var resErr *ResInfoError
resAir, resErr = morexhw.GetAirPortXEquip(conf.SysProfileCmd.KeySysProfile, conf.SysProfileCmd.ArgsSysProfile[0])
resErrs = append(resErrs, resErr)
}()
}
if xhw == "appsxequip" {
go func() {
defer wg.Done()
var resErr *ResInfoError
resApp, resErr = morexhw.GetAppsXEquip(conf.SysProfileCmd.KeySysProfile, conf.SysProfileCmd.ArgsSysProfile[1])
resErrs = append(resErrs, resErr)
}()
}
if xhw == "audioxequip" {
go func() {
defer wg.Done()
var resErr *ResInfoError
resAudio, resErr = morexhw.GetAudioXEquip(conf.SysProfileCmd.KeySysProfile, conf.SysProfileCmd.ArgsSysProfile[2])
resErrs = append(resErrs, resErr)
}()
}
if xhw == "powerxequip" {
go func() {
defer wg.Done()
var resErr *ResInfoError
resPower, resErr = morexhw.GetPowerXEquip(conf.SysProfileCmd.KeySysProfile, conf.SysProfileCmd.ArgsSysProfile[10])
resErrs = append(resErrs, resErr)
}()
}
}
morexhw = &HWXEquipData{
AirPortData: resAir,
AppsData: resApp,
AudioData: resAudio,
PowerData: resPower,
}
return morexhw, resErrs
}
I called the goroutine directly in the main function, then everything works and the result is displayed, but I wanted it through the interface