go http client发送请求
1. 简单并发
package main
import (
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
)
func main() {
var numberTasks = [5]string{"13456755448", " 13419385751", "13419317885", " 13434343439","13438522395"}
client = &http.Client{}
beg := time.Now()
wg := &sync.WaitGroup{}
for _, keyword := range numberTasks{
wg.Add(1)
//go func(keyword string, group *sync.WaitGroup) {
func(keyword string, group *sync.WaitGroup) {
body, err := NumberQueryRequest(keyword)
if err != nil{
fmt.Printf("error occurred in query keyword: %s, error: %s\n",keyword, err.Error() )
}else{
fmt.Printf("search %s success, data size is %d\n, body is %s\n",keyword, len(body), string(body))
}
group.Done()
}(keyword, wg)
}
wg.Wait() //waitGroup在主线程等待所有任务完成
fmt.Printf("time consumed: %fs", time.Now().Sub(beg).Seconds())
}
var client *http.Client
//GET请求的通用写法, 注意复用client即可.
func NumberQueryRequest(keyword string)(body []byte, err error){
url := fmt.Sprintf("https://api.binstd.com/shouji/query?appkey=df2720f76a0991fa&shouji=%s", keyword)
req, err := http.NewRequest("GET", url, nil)
if err != nil{
return nil, err
}
//resp, err := client.Get(url)
resp, err := client.Do(req)
fmt.Printf("你好啊,req=%v\n",req)
if err != nil{
return nil, err
}
if resp.StatusCode != http.StatusOK{
data, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("response status code is not OK, response code is %d, body:%s", resp.StatusCode, string(data))
}
if resp != nil && resp.Body != nil{
defer resp.Body.Close()
}
body, err = ioutil.ReadAll(resp.Body)
if err != nil{
return nil, err
}
return body, nil
}
2. master-worker形式
package main
import (
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
)
const (
routineCountTotal = 5 //限制线程数
)
func main() {
var numberTasks = [5]string{"13456755448", " 13419385751", "13419317885", " 13434343439", "13438522395"}
client = &http.Client{}
beg := time.Now()
wg := &sync.WaitGroup{}
tasks := make(chan string)
results := make(chan string)
//receiver接受响应并处理的函数块, 也可以单独写在一个函数
go func() {
for result := range results{
if result == ""{
close(results)
}else{
fmt.Println("result:", result)
}
}
}()
for i:= 0; i < routineCountTotal; i++{
wg.Add(1)
go worker(wg, tasks, results)
}
//分发任务
for _, task := range numberTasks{
tasks <- task
}
tasks <- "" //worker结束标志
wg.Wait() //同步结束
results <- "" // result结束标志
fmt.Printf("time consumed: %fs", time.Now().Sub(beg).Seconds())
}
func worker(group *sync.WaitGroup, tasks chan string, result chan string){
for task := range tasks{
if task == ""{
close(tasks)
}else{
respBody, err := NumberQueryRequest(task)
if err != nil{
fmt.Printf("error occurred in NumberQueryRequest: %s\n", task)
result <- err.Error()
}else{
result <- string(respBody)
}
}
}
group.Done()
}
var client *http.Client
func NumberQueryRequest(keyword string) (body []byte, err error) {
url := fmt.Sprintf("https://api.binstd.com/shouji/query?appkey=df2720f76a0991fa&shouji=%s", keyword)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")
resp, err := client.Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
data, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("response status code is not OK, response code is %d, body:%s", resp.StatusCode, string(data))
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
3. 使用带缓冲区的chan控制
package golimit
type Glimit struct {
Num int
C chan struct{}
}
func NewG(num int) *Glimit {
return &Glimit{
Num: num,
C : make(chan struct{}, num),
}
}
func (g *Glimit) Run(f func()){
g.C <- struct{}{}
go func() {
f()
<-g.C
}()
}
写入自己的博客中才能记得长久