Using the net/http package in go

When I started picking up the go language years ago, things that stood out to me immediately were the io and net/http packages. Both of those packages were extremely well done and minimal in the amount of interfaces they exposed to the user.

What I love about net/http

The two things which stand out for me are:

Interface Design

The primary constructs you’ll interact with are pretty limited:

To build your service:

http.Handler
http.HandlerFunc
http.Server

To handle requests:

http.ResponseWriter
http.Request

To build a client:

http.Client
http.Transport

In total, we’re looking at 7 core primitives to build a client and server — which isn’t a lot!

Composability

When it comes to building HTTP services, the main things users need likely fall into two buckets:

Here’s an example showing this composability in action [1] [2]:

Testing your service

From this example, testing your service would be pretty straightforward:

Conclusion

As we can see, getting started with the net/http package is extremely simple but packs a lot of punch.

In a followup article, I’ll share more about using http.Client as the unit of integration for building HTTP clients.

Footnotes

software engineer at auth0, writing code for humans, with humans.