Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[日常] GO语言圣经-并发获取多个URL大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

go语言圣经-并发获取多个URL

1.GO最新奇的特性就是对并发编程的支持,goroutIne和chAnnel

2.goroutIne是一种函数的并发执行方式,而chAnnel是用来在goroutIne之间进行参数传递

  go function则表示创建一个新的goroutIne,并在这个新的goroutIne中执行这个函数

3.make函数创建了一个传递String类型参数的chAnnel

4.io.Copy把响应的Body内容拷贝到IoUtil.Discard输出流中,扔到一个垃圾

5.goroutIne尝试在一个chAnnel上做send或者receive操作时,这个goroutIne会阻塞在调用处,直到另一个goroutIne往这个chAnnel里写入、或者接收值

6.用main函数来接收所有fetch函数传回的字符串,可以避免在goroutIne异步执行还没有完成时main函数提前退出

练习 1.10: 找一个数据量比较大的网站,用本小节中的程序调研网站的缓存策略,对每个URL执行两遍请求,查看两次时间是否有较大的差别,并且每次获取到的响应内容是否一致,修改本节中的程序,将响应结果输出,以便于进行对比。

练习 1.11: 在fetchall中尝试使用长一些的参数列表,比如使用在alexa.com的上百万网站里排名靠前的。如果一个网站没有回应,程序将采取怎样的行为?(Section8.9 描述了在这种情况下的应对机制)。

fetchall.go

Highlighter">

//导入io/IoUtil和net/Http包
import (
"fmt"
"io"
"os"
"io/IoUtil"
"net/http"
"Strings"
"time"
)

func main() {
//当前时间
start := time.Now()
//make函数创建了一个传递String类型参数的chAnnel
ch := make(chan String)
//for循环命令行参数
for _,url := range os.Args[1:] {
//开启一个goroutIne
go fetch(url,ch)
}
//接收并打印chAnnel,for循环不需要key value
for range os.Args[1:]{
fmt.Printf(<-ch)//注意箭头方向,接收chAnnel
}
//main函数的时间
fmt.Printf("main: %.2fs \n",time.Since(start).Seconds())
}
//参数类型:string,chan<- String
func fetch(url String,ch chan<- String){
start := time.Now()
//加入前缀
if !Strings.HasPrefix(url,"http://") {
url = "http://" + url
}
res,err := http.Get(url)
//判断错误
if err != nil {
//向标准错误流打印信息
fmt.Fprintf(os.Stderr,"fetch:%v \n",err)
//终止进程
os.Exit(1)
}
//输出http code
//fmt.Printf("\nhttp status code :%s\n",res.Status)

            //把<a href="http://code.js-code.com/tag/neirong/" target="_blank" class="keywords">内容</a>扔掉,只<a href="http://code.js-code.com/tag/huoqu/" target="_blank" class="keywords">获取</a>字节数
            nBytes,err:=io.Copy(<a href="http://code.js-code.com/tag/IoU/" target="_blank" class="keywords">IoU</a>til.Discard,res.body)
            //判断<a href="http://code.js-code.com/tag/cuowu/" target="_blank" class="keywords">错误</a>
            if err != nil {
                    //向标准<a href="http://code.js-code.com/tag/cuowu/" target="_blank" class="keywords">错误</a>流打印信息
                    fmt.Fprintf(os.Stderr,err)
                    //终止进程
                    os.Exit(1)
            }   
            res.body.Close()
            //记录执行的秒数
            sec := time.Since(start).Seconds()
            //发送给chAnnel
            ch<- fmt.Sprintf("%.2fs %7d <a href="http://code.js-code.com/tag/s/" target="_blank" class="keywords">%s</a> \n",sec,nBytes,url)      

}

对比fetch.go

Highlighter">

//导入io/IoUtil和net/Http包
import (
"fmt"
"io"
"os"
"io/IoUtil"
"net/http"
"Strings"
"time"
)

func main() {
//开始时间
start := time.Now()
//for循环命令行参数
for _,url := range os.Args[1:] {
//加入前缀
if !Strings.HasPrefix(url,err)
//终止进程
os.Exit(1)
}

            //body,err := <a href="http://code.js-code.com/tag/IoU/" target="_blank" class="keywords">IoU</a>til.ReadAll(res.body)
            //避免申请<a href="http://code.js-code.com/tag/yige/" target="_blank" class="keywords">一个</a>缓冲区,直接到标准<a href="http://code.js-code.com/tag/shuchu/" target="_blank" class="keywords">输出</a>流
            nBytes,err)
                    //终止进程
                    os.Exit(1)
            }   
            res.body.Close()
            //<a href="http://code.js-code.com/tag/shuchu/" target="_blank" class="keywords">输出</a>http code
            fmt.Printf("\nhttp status code :<a href="http://code.js-code.com/tag/s/" target="_blank" class="keywords">%s</a> %7d <a href="http://code.js-code.com/tag/s/" target="_blank" class="keywords">%s</a>\n",res.Status,url)
            //fmt.Printf("<a href="http://code.js-code.com/tag/s/" target="_blank" class="keywords">%s</a>",body)
    }   
    //结束时间
    sec:=time.Since(start).Seconds()
    fmt.Printf("main:%.2fs\n",seC)

}

执行效果对比:

[日常] GO语言圣经-并发获取多个URL

  

大佬总结

以上是大佬教程为你收集整理的[日常] GO语言圣经-并发获取多个URL全部内容,希望文章能够帮你解决[日常] GO语言圣经-并发获取多个URL所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: