package main import ( "bufio" "fmt" "net/http" "os" "sync" ) var ( maxWorkers = 10 wg sync.WaitGroup ) func worker(in <-chan string) { for url := range in { resp, err := http.Get(url) if err != nil { fmt.Printf("Failed to request %s\n", url) } else { defer resp.Body.Close() fmt.Printf("URL %s has been successfully requested\n", url) } wg.Done() } } func main() { file, err := os.Open("url.txt") if err != nil { fmt.Println("Failed to open file:", err) return } defer file.Close() // 创建一个带缓冲的通道 urls := make(chan string, maxWorkers) // 启动goroutines处理请求 for i := 0; i < maxWorkers; i++ { go worker(urls) } scanner := bufio.NewScanner(file) // 将URL发送给 goroutines 处理 for scanner.Scan() { url := scanner.Text() wg.Add(1) urls <- url } close(urls) wg.Wait() }