HiveBrain v1.2.0
Get Started
← Back to all entries
snippetgoCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howsetforgolangtimeoutrequestshttpget

Problem

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_err := http.Get(url)


How can I set a custom timeout for each Get request? (The default time is very long and that makes my fetcher really slow.) I want my fetcher to have a timeout of around 40-45 seconds after which it should return "request timed out" and move on to the next URL.

How can I achieve this?

Solution

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.

Code Snippets

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

Context

Stack Overflow Q#16895294, score: 399

Revisions (0)

No revisions yet.