创建所需数量的工作线程,而不是每个URL一个工作线程:
parallel := flag.Int("parallel", 10, "max parallel requests allowed")flag.Parse()// Workers get URLs from this channelurls := make(chan string)// Feed the workers with URLsgo func() { for _, u := range flag.Args() { urls <- u } // Workers will exit from range loop when channel is closed close(urls)}()var wg sync.WaitGroupclient := rest.Client{}results := make(chan string)// Start the specified number of workers.for i := 0; i < *parallel; i++ { wg.Add(1) go func() { defer wg.Done() for url := range urls { worker(url, client, results) } }()}// When workers are done, close results so that main will exit.go func() { wg.Wait() close(results)}()for res := range results { fmt.Println(res)}


