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


http package to work with http protocol


creates HTTP client object


sets request timeout

3 * time.Second

timeout is set to 3 seconds


object to access response body

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

output response body to stdout