How to Enable HTTP Keep Alive in Go

  • Share http transport — you can new http client every request but http transport must be shared.
  • Read all response body.

io.Copy(ioutil.Discard, resp.Body)

  • Response body must be closed.

resp.Body.Close()

  • Set http header with “Connection: keep-alive”.

req.Header.Set("Connection", "keep-alive")