今天,我了解了.我所理解的問題是,我正在使用 WaitGroup 的函數(shù)來執(zhí)行所有戈魯廷。buffered channelsWait但并非所有的戈魯廷都在執(zhí)行,程序在沒有等待所有戈魯廷完成的情況下結(jié)束。法典:func main() { // initializing a WaitGroup var wg sync.WaitGroup // adding 3 counts/buffer to the WaitGroup wg.Add(3) fmt.Println("Start Goroutines") go responseSize("https://www.golangprograms.com", &wg) go responseSize("https://stackoverflow.com", &wg) go responseSize("https://coderwall.com", &wg) // wait for goroutines to finish wg.Wait() fmt.Println("Terminating the main program")}// just prints the response size of the body returned func responseSize(url string, wg *sync.WaitGroup) { // schedule the Done() call when the goroutine is finished wg.Done() fmt.Println("Step1: ", url) response, err := http.Get(url) if err != nil { log.Fatal(err) } fmt.Println("Step2: ", url) body, err := ioutil.ReadAll(response.Body) if err != nil { log.Fatal(err) } fmt.Println("Step3: ", len(body))}我在這里錯過了什么嗎?
圍棋戈魯丁沒有運(yùn)行
慕尼黑5688855
2022-09-26 15:28:59