Correct waitgroup goroutine in nested method in main function

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

Make your Goroutines call Done() on the WaitGroup. The internet has many examples how to do this.

Yes, this is the problem that Done() is called in the goroutines and the main goroutine is waiting for them to be executed, but their result is empty, although if you do not use the goroutines, but simply execute one by one, then the results are there, which means I’m not using Done() somewhere

Maybe someone will come in handy, don’t be stupid like me.
Everything, figured out, it turns out that WaitGroup has nothing to do with it, at first everything is written correctly, I needed to use channels for output, the main function is also a goroutine, and channels are used between goroutines for communication to receive the sent values.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.