funcmain(a){ // Create an empty structure channel with size 512 channel := make(chanstruct{},512) / / create a sync. WaitGroup var wg sync.WaitGroup // Wg is the length of Uins wg.Add(len(Uins)) for_,v:= range Uins{ channel <- struct{} {}// Pass WG as a parameter to the coroutine go getUserInfo(v,channel,&wg) } // block until wg is 0 wg.Wait() return }
funcgetUserInfo(v int,channel chanstruct{},wg *sync.WaitGroup){ deferfunc(a) { // Wargaming is reduced by 1 before return wg.Done() <-channel }() /** 业务逻辑 **/ }