基本上,您生成了数百个goroutine,这些goroutine将在块关闭之前开始连接。
这是一个快速(非常丑陋)的代码:
var ( responseStatuses = make(map[int]int, 63) reqsDone = 0 urlCh = make(chan string, numConcurrent) ch = make(chan *http.Response, numConcurrent))log.Println(numConcurrent, numRequests, len(responseStatuses))for i := 0; i < numConcurrent; i++ { go func() { for url := range urlCh { client := &http.Client{} req, reqErr := http.NewRequest("GET", url, nil) if reqErr != nil { fmt.Println(reqErr) } // adding connection:close header hoping to get rid // of too many files open error. Found this in http://craigwickesser.com/2015/01/golang-http-to-many-open-files/ req.Header.Add("Connection", "close") resp, err := client.Do(req) if err != nil { fmt.Println(err) } ch <- resp } }()}go func() { for i := 0; i < numRequests; i++ { urlCh <- url } close(urlCh)}()playground



