Using the net/http package in go

What I love about net/http

  1. Interface Design.
  2. Composability

Interface Design

http.Handler
http.HandlerFunc
http.Server
http.ResponseWriter
http.Request
http.Client
http.Transport

Composability

  1. Defining what handlers to run based on a method, or path.
  2. Defining middleware to be executed.
  • In this miniature example, we can see that the authenticate middleware is doing authentication for us.
  • Similarly, we handle path routing by specifying the prefix to use for each handler.

Testing your service

Conclusion

Footnotes

  • [1]: For brevity, we’ve chosen to use http.HandlerFunc in our code example, but in practice, or in production, we’d always want to use http.Handler since we can convert an http.HandlerFunc to it easily.
  • [2]: Similarly, we would want to handle graceful shutdown of the http server so we’d want to utilize the Shutdown method of the http.Server primitive.

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Visual Introduction to Git

What is my developer going on about? — A techie talk glossary.

Disadvantages of scripting languages

CODE Review June 21st

LeetCode September Challenge(Day-9)

A simple yet powerful “List” in C

On testing in DDD

March 2018: GitHub Development Winners

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cyril David

Cyril David

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

More from Medium

Reading Configuration Files in Go

Getting started with GO Programming Language — Part Two

Golang Intellij Idea setup

Battle of Concurrency | Goroutines vs Threads.