A story about Go http.Client

José Coelho
Aug 8, 2017 · 1 min read

Or how I have learned to embrace http.RoundTripper

Using a http client in Go usually starts like this:

Everything works, until it doesn’t: a network blip, a connection reset, a slow response, etc.

After some research (I recommend reading The complete guide to Go net/http timeouts) you may end up writing something similar to:

Ok! But now the question is: how does http.Client really work?

Ok… what is the RoundTripper?

Way simpler than I would expect: for each request, it receives a response, if everything works.

The default transport.go implementation is interesting. However, what else could I use RoundTripper for?


Implementing a naïve Retry RoundTripper without any concurrency concerns or request copy:

The HTTP retry client continues retrying until it succeeds or the context timeout reached.


A very simple http client cache:


RoundTripper interface enables extending the HTTP behaviour in a composable way.

The more I learn about Go Standard Library, the more I acknowledge the attention and craft put into it.

Thanks to Manuel Tiago Pereira and Vanessa Correia

    José Coelho

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade