net/http package. This example demonstrates how to issue simple HTTP requests.
Basic HTTP GET Request
The simplest way to make an HTTP request is usinghttp.Get, which is a convenient shortcut around creating an http.Client object.
Key Concepts
http.DefaultClient
http.DefaultClient
The
http.Get function uses the default HTTP client which has sensible default settings for timeouts, redirects, and connection pooling.Response Body Handling
Response Body Handling
Always remember to close the response body with
defer resp.Body.Close() to prevent resource leaks.Error Handling
Error Handling
HTTP requests can fail for many reasons (network issues, DNS problems, etc.), so always check for errors returned by
http.Get.Running the Example
The example prints the HTTP status code and the first 5 lines of the response body.
Next Steps
HTTP Server
Learn how to build HTTP servers in Go
Context
Control request cancellation with context