How to set timeout for http.Get() requests in Golang?

后端 未结 7 1382
刺人心
刺人心 2020-12-22 16:15

I\'m making a URL fetcher in Go and have a list of URLs to fetch. I send http.Get() requests to each URL and obtain their response.

resp,fetch_e         


        
7条回答
  •  [愿得一人]
    2020-12-22 16:37

    Apparently in Go 1.3 http.Client has Timeout field

    client := http.Client{
        Timeout: 5 * time.Second,
    }
    client.Get(url)
    

    That's done the trick for me.

提交回复
热议问题