type loggingTransport struct { // Embed the default transport to use its default methods. http.RoundTripper } func (t *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error) { log.Print("Making request to: ", req.URL.String()) // Call the underlying RoundTrip implementation to make the request. res, err := t.RoundTripper.RoundTrip(req) log.Printf("Received response with status code %d", res.StatusCode) return res, err } // Create an HTTP client that uses our custom RoundTripper. client := &http.Client{ Transport: &loggingTransport{http.DefaultTransport}, } // Make an HTTP request. res, err := client.Get("http://example.com/")
type cachingTransport struct { cache map[string]*http.Response http.RoundTripper } func (t *cachingTransport) RoundTrip(req *http.Request) (*http.Response, error) { key := req.URL.String() // Check if the response is already in the cache. if res, ok := t.cache[key]; ok { return res, nil } // Call the underlying RoundTrip implementation to make the request. res, err := t.RoundTripper.RoundTrip(req) // Cache the response for future use. t.cache[key] = res return res, err } // Create an HTTP client that uses our custom RoundTripper. client := &http.Client{ Transport: &cachingTransport{ cache: make(map[string]*http.Response), RoundTripper: http.DefaultTransport, }, } // Make an HTTP request. res, err := client.Get("http://example.com/")In both examples, we define a custom RoundTripper that wraps the default HTTP transport and adds some additional functionality. We then use this custom RoundTripper when creating an HTTP client. Package Library: The `net/http` package is a core library that is included with Go.