package main import ( "fmt" "net/http" ) func main() { client := &http.Client{} req, err := http.NewRequest("GET", "https://www.google.com/", nil) if err != nil { panic(err) } resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() fmt.Println(resp.Status) }
package main import ( "fmt" "net/http" "time" ) func main() { transport := &http.Transport{ Dial: (&net.Dialer{ Timeout: 5 * time.Second, }).Dial, TLSHandshakeTimeout: 5 * time.Second, } client := &http.Client{ Timeout: time.Second * 10, Transport: transport, } req, err := http.NewRequest("GET", "https://www.google.com/", nil) if err != nil { panic(err) } resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() fmt.Println(resp.Status) }This example shows how to create an `http.Client` with a custom `http.Transport` that has a timeout of 5 seconds for both dialing and TLS handshake. The `http.Client` itself has a timeout of 10 seconds. If the server does not respond within 10 seconds, the request will fail. Package library: `net/http`