Mux Ado About Routing

Learn how to use established third-party HTTP router libraries to build mature HTTP APIs.

Better routing

Until now, we were using the default HTTP mux and writing handlers to build what is commonly called a router.

Why a router? Well, it clearly defines all the paths incoming traffic can take and then proceeds to manage the traffic by sending it to the appropriate handlers! Seems like a pretty apt name. Routing is one of the most fundamental and crucial aspects of serving web traffic, and we will not try to reinvent the wheel here. While the base packages do their job honorably, getting the complete feature set of bells and whistles that we would want is tedious using just the barebones vanilla library. That is where several third-party libraries step in and give us powerful, easy-to-use, and blazingly fast APIs that are intuitive and help us write cleaner and better code faster.

We have been using the chi router for half a decade, and while Go has evolved considerably in that time, chi has never faltered and has matched it stride for stride. It is fast, robust, elegant, relies only on standard libraries, and is, therefore, very reliable. For this course, we will be using chi as our router. Nevertheless, please feel free to explore other Go HTTP router libraries, but tread with caution.

Let’s look at what our router will look like with chi.
Now, you know the routine. Go through and run the code below, wait for it to start in the run terminal, open a new terminal, run the curl localhost:8080 command, and return to the run terminal to see the logs.

Get hands-on with 1400+ tech skills courses.