package main
import (
"fmt"
"io/ioutil"
"net/http"
"sync"
)
// 定义请求结果的结构体
type Response struct {
URL string
Body []byte
Error error
}
func main() {
// 假设我们有10个网址
urls := []string{
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=2104061240261111198579",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=2104231510031401001",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=210513142437820140",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=2106011528471953957",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=210617100209726953",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=2106171533531111285065",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=2107092127301111202822",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=21080811074988808",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=2108251724531112183027",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=2112081655171111777555",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=2203232020501112787513",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=2203241534391113388373",
"http://simcrm.jinyingjie.com/?m=api&a=dealOrderDataMove&order_num=2205060820161111695191",
// "https://www.baidu.com",
}
var wg sync.WaitGroup
responses := make(chan Response, len(urls)) // 创建一个buffered channel来接收响应
// 发起并发请求
for _, url := range urls {
wg.Add(1) // 递增WaitGroup的计数器
go func(url string) {
defer wg.Done() // 请求完成后递减WaitGroup的计数器
resp, err := http.Get(url)
if err != nil {
responses <- Response{URL: url, Error: err}
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
responses <- Response{URL: url, Error: err}
return
}
responses <- Response{URL: url, Body: body}
}(url)
}
// 等待所有请求完成
go func() {
wg.Wait()
close(responses) // 所有请求完成后关闭channel
}()
// 处理响应
for response := range responses {
ifresponse.Error!= nil {
fmt.Printf("Error fetching %s: %v\n", response.URL, response.Error)
continue
}
fmt.Printf("Fetched %s,body %s, body length: %d\n", response.URL, string(response.Body), len(response.Body))
// 在这里可以进一步处理响应体,比如解析JSON等
}
}