Develop REST APIs in Go using Gorilla Mux

Bijesh O S
Geek Culture
Published in
4 min readJul 5, 2021

An introduction on how to develop REST APIs in Go using Gorilla Mux

Photo by Markus Spiske on Unsplash

Introduction

REST APIs are an integral part of today’s programmer’s life. These days, most of the communication between various software components happen via REST APIs. Prominent programming languages have a wide variety of libraries and frameworks which help programmers to reduce time to build API services from scratch.

In this article, we are going to explore how to develop REST APIs in Go language using Gorilla Mux.

What is Gorilla Mux

Gorilla Mux is a package from the Gorilla Web Toolkit. It provides simple ways to create HTTP routers. Using the package, we can redirect program flow to respective HTTP handler functions.

Why is it called Mux? The name is derived from HTTP request multiplexer.

Gorilla Web Toolkit

Gorilla is a web toolkit for Go programming language which consists of multiple packages. Gorilla Mux is one package in the Gorilla Web Toolkit. Other packages include Gorilla RPC and Gorilla WebSocket. For more details, refer here.

Project

Let’s start with creating a project structure first.

Create a directory for the project (let’s name it as go-rest-api-example). Now, execute the following to initialize Go modules.

go mod init get go-rest-api-example

This will initialize Go module and create a file named go.mod which keeps track of dependencies.

Install

Installation of Gorilla Mux is quite straight forward. Execute the following command from the project directory:

go get -u github.com/gorilla/mux

Once executed, this will download Gorilla Mux and configure as a dependency. (At the time of writing, latest version of Gorilla Mux is v1.8.0)

Now, let’s proceed to build simple API end points.

What are we developing ?

For the purpose o this article, let’s pick a simple example. We’ll create two API end points:

  • GET /health-check : This will return a message stating that API server is running. If we do not get any response, we can assume that the API server is not up.
  • GET /persons : This will return a JSON message which contains array of person details.

Response Structure

Let’s define the response structure as follows:

REST API Response Structure

Here, we define a person’s details and response as an array of persons. Since we would like the response to be JSON, we’ll add JSON mapping for each field as well.

Routes

Now, it’s time to specify API endpoints, handler functions and HTTP methods.

We can do it as follows:

Configure REST API routes

The API server is at localhost and port 8080.

Handler Functions

Now, we need to define handler functions for the respective end points defined above.

Health Check

The handler function accepts both request and response parameters. We’ll set a status header and a simple response as shown below.

Health Check handler function

Persons

In the handler function for persons API end point, we’ll set the response type to be JSON. Once the persons details are retrieved from a helper function, we’ll covert the structure to JSON and add it as a response.

Usually, we would fetch the person these details from a database. For the time being, let’s use a simple way to get the person details as follows:

That’s it. Our code is ready now.

You can find complete working version of the code here.

Execution

Let’s execute the program as follows:

go run main.go

You would be able to see the logs as follows:

Now, our API server is running and ready to take requests.

Testing

For testing the API end points, let’s use Postman. If you do not have it already, you can get it from here.

Health Check

Let’s access health check end point using the following URL:

http://localhost:8080/health-check

Result is as follows:

Persons

Now, let’s access persons end point using the following URL:A

http://localhost:8080/persons

Result is as follows:

Logs

You can also see the logs on the terminal as follows:

Summary

In this article, we created two simple REST API end points in Go using Gorilla Mux.

Working version of the project can be found at the following GitHub repo:

https://github.com/bijeshos/go-rest-api-example

--

--