4 Go Web Routers for Your Next Web App

Web routing is a technique of assigning HTTP requests to handler functions that operate on the specified URL path. Routing is often used to build single-page applications (SPAs) and APIs. In the router, the code defines the sequence of actions accompanying a user’s request.

The Go standard library has most of the functionality you’ll need to build web applications, including routing. You can use theServeMuxtype in thenet/httppackage for basic route handling. If you need more complex route handling, there are plenty of routing packages to choose from.

4

Getting Started With Routing in Go

Go’s ecosystem is home to routing packages that abstract the unnecessary and make it easier to build web applications and services.

Many Go web frameworks implement some of these routing packages.

The

Here’s a simple struct model for theJSONresponse you’ll return to the client in this tutorial.

Most Go routers still use the net/httpResponseWriterandRequestmethods as parameters in handler functions.

Soundbar and TV

Also, you’ll need to start a server with theListenAndServemethod to start up the server in most packages.

Here are some of the popular routing packages in the Go ecosystem.

The streaming app Filmrise opened to crime documentaries and suspense movies and new arrivals

1. The Gorilla Mux Router

The Gorilla Mux package implements both a request router and a request dispatcher for HTTP requests. It’s like thehttp.ServeMuxmethod but with added functionality.

The Gorilla Mux router implements thehttp.Handlerinterface and its methods are compatible with theServeMuxmethod. The package also implements multiple URL schemes and you’re able to use routers as sub routers (nested routes) to define similar routes.

HTML source code syntax highlighted

TheGorilla Muxpackage is part of the Gorilla web toolkit. The toolkit includes web-related packages to solve many problems fromkeeping users logged in with Sessionsto data storage using Cookies.

Run this command in the terminal of your workspace to install the Gorilla Mux package.

Here’s how you can set up aGETrequest route to a handler function that encodes to a JSON response with the Gorilla Mux package.

Thehellohandler function encodes the struct to JSON using theEncodemethod of theNewEncoderstruct.

Themainfunction assigns a new Gorilla Mux router instance to theroutervariable. It then calls theHandleFuncmethod to map the route to the handler function. You can use theMethodsmethod to specify a request type.

2. The Chi Router

TheChi Routeris a lightweight, fast, composable router for building HTTP-based web services in Go. The Chi router is compatible with thehttppackage, and there are no external dependencies for the router.

Unlike many routers, Chi provides context control with thecontextpackage for timely request handling.

Install the Chi router to your Go modules with this command.

Routing with the Gorilla Mux router and the Chi router is very similar. Here’s how you can set up a similarGETrequest that encodes struct to JSON as a response.

Thehellohandler function is the handler function for theGETrequest.

In themainfunction, theroutervariable is a Chi router instance. You can specify a request method with the method name; in this case,Gettakes in the route and handler function identifier as parameters.

3. The HttpRouter Package

TheHttpRouterpackage is a highly performant, lightweight request multiplexer. It provides more functionality than theServeMuximplementation of the http package.

The HttpRouter package has a small memory footprint, high performance, and good scalability. It’s one of the oldest and most used routers in the Go ecosystem, implemented in manypopular Go frameworks, includingthe Gin library.

Running this command on the terminal of your working directory will install the HttpRouter package.

Routing with the HttpRouter differs slightly from the Chi and Gorilla Mux routers.

Here’s how it’s possible to set up a simple GET request with the HttpRouter package.

Handler functions for the HttpRouter package must have theParamsmethod of the HTTP router.

Theroutervariable is an instance of HttpRouter. You can set up a GET request with theGETmethod that takes in the route and the handler function identifier.

4. The Pat Router

Patis a Sinatra-style HTTP request multiplexer that works with Go’s net/http package. The Pat router doesn’t have any functionalities other than routing.

Run this command in your working directory to install the Pat Router

The Pat router implements similar functionalities to theServeMuxmethod.

Here’s how you can handle request routing with thepatpackage.

TheNewmethod returns a router instance. You’ll have to wrap the handler function with theHandlerFuncmethod. You can then use theHandlemethod to specify a root path and mount the router instance before starting a server.

Building a Router Isn’t That Difficult

All the routers in this article implement functionality from the net and http packages. You can check out the documentation of the ServeMux method to learn how to replicate a router with extra functionality to suit your needs.

Frameworks like Gin and Fiber include routing functionality from the net package or external packages that implement it.

Want to use Golang for your next web app? Look no further than this selection of top-notch frameworks to help with the job.

Make sure you don’t miss these movies and shows before Netflix removes them.

It saves me hours and keeps my sanity intact.

Some subscriptions are worth the recurring cost, but not these ones.

OneDrive is one of the best, but it has a catch.

When your rival has to bail out your assistant.

Technology Explained

PC & Mobile