Skip to content

Instantly share code, notes, and snippets.

@erasin
Created September 18, 2013 02:52
Show Gist options
  • Save erasin/6603872 to your computer and use it in GitHub Desktop.
Save erasin/6603872 to your computer and use it in GitHub Desktop.
为http.client.get 添加timeout
var timeout = time.Duration(2 * time.Second)
func dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, timeout)
}
func main() {
transport := http.Transport{
Dial: dialTimeout,
}
client := http.Client{
Transport: &transport,
}
resp, err := client.Get("http://some.url")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment