To make an HTTP HEAD request in Go, we can use the standard library's http
package. Here's how to do it:
main.go529 chars25 lines
In this code, we first create a new http.Request
object using the http.NewRequest
function, passing in the HTTP method (HEAD
), the URL to request, and an optional request body (which we don't need for a HEAD request).
We then use the http.DefaultClient
to perform the request using the client.Do(req)
method. This returns an http.Response
object that we can inspect for the result of our request.
Finally, we close the response body using defer resp.Body.Close()
, and then do something with the response (in this case, print out the response status).
gistlibby LogSnag