这段代码的目的是使用 goroutine 计算我的电脑和 google.com 之间的每个连接时间。但结果似乎错误。为什么时间一直在增加。这是怎么回事?2013/12/04 16:10:41 www.google.com 597.175072ms2013/12/04 16:10:41 www.google.com 608.161898ms2013/12/04 16:10:41 www.google.com 614.527441ms2013/12/04 16:10:41 www.google.com 620.51907ms2013/12/04 16:10:41 www.google.com 630.052257ms2013/12/04 16:10:42 www.google.com 654.539717ms2013/12/04 16:10:42 www.google.com 659.144724ms2013/12/04 16:10:42 www.google.com 1.282230659s2013/12/04 16:10:42 www.google.com 1.353469764spackage mainimport ( "log" "net" "net/http" "time" "sync")var wg sync.WaitGroupfunc main() { url := "http://www.google.com" for i := 0; i < 9; i++ { wg.Add(1) go request(url) } wg.Wait()}func dialTimeout(network, addr string) (net.Conn, error) { return net.DialTimeout(network, addr, time.Duration(1*time.Second))}func request(url string) { var t0, t1 time.Time transport := http.Transport{ Dial: dialTimeout, } client := http.Client{ Transport: &transport, } t0 = time.Now() _, err := client.Get(url) t1 = time.Now() if err != nil { log.Println(err) } log.Println(url +" "+ t1.Sub(t0).String()) wg.Done()}
1 回答
- 1 回答
- 0 关注
- 168 浏览
添加回答
举报
0/150
提交
取消