9951 explained code solutions for 126 technologies


golangHow to set request timeout in HTTP client


package main

import ("net/http"; "os"; "io"; "time")

func main() {
  h := http.Client{Timeout: 3 * time.Second}
  r, _ := h.Get("https://echoof.me/")
  defer r.Body.Close()
  io.Copy(os.Stdout, r.Body)
}ctrl + c
package main

default package declaration

net/http

http package to work with http protocol

http.Client{

creates HTTP client object

Timeout:

sets request timeout

3 * time.Second

timeout is set to 3 seconds

.Body

object to access response body

io.Copy(os.Stdout, r.Body)

output response body to stdout