码迷,mamicode.com
首页 > 其他好文 > 详细

Go从三个站点中返回响应最快的

时间:2018-01-11 15:54:02      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:利用   ack   ring   googl   get   pos   turn   make   pack   

利用协程可以轻松实现

package main

import (
    "fmt"
    "github.com/imroc/req"
)

func mirroredQuery() string {
    responses := make(chan string, 3)
    go func() { responses <- request("http://www.baidu.com") }()
    go func() { responses <- request("http://www.google.com") }()
    go func() { responses <- request("http://www.qq.com") }()

    return <-responses
}

//发送http请求方法
func request(hostname string) (response string) {
    r := req.New()
    r.Get(hostname)
    return hostname
}

func main() {
    fmt.Println(mirroredQuery())
}

  

Go从三个站点中返回响应最快的

标签:利用   ack   ring   googl   get   pos   turn   make   pack   

原文地址:https://www.cnblogs.com/chenqionghe/p/8267267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!