CRUD OPERATIONS USING GOLANG with Gorilla Mux

Pulkit Kaushik
4 min readFeb 10, 2021

When it comes to using robust software development programming language, Golang has been chosen by many people. This programming language has its own unique benefits to offer the programmers. “Go” is basically a compiled language, which takes the inspiration from the syntax of C. So, if you are well-versed with C syntax, you can find Golang to be useful. It will take only a few days to learn this programming language, as syntax is similar to the “C” programming language. Today, Golang has been used by renowned companies, like Netflix, Uber, Facebook, Dropbox, etc. It is expected to grow as an extensively used programming language in future.

Lets start the discussion of how we can perform CRUD Operations using Golang with mux router.

I will be using VScode to achieve this task any other text editor can also be used.

Make sure before proceeding : Install Golang on your machine, Install VScode extension of Golang for easy development.

Use case- This article will focus on a simple idea of storing the books written by particular author.

Important imports :

encoding/json-Go offers built-in support for JSON encoding and decoding, including to and from built-in and custom data types.

math/rand- generate a random number(id in this case)

net/http- Package http provides HTTP client and server implementations.Get, Head, Post, and PostForm make HTTP (or HTTPS) requests.

strconv- Package strconv implements conversions to and from string representations of basic data types.

gorilla/mux- Implements a request router and dispatcher for matching incoming requests to their respective handler. The name mux stands for “HTTP request multiplexer”.

log-Package log implements a simple logging package. It defines a type, Logger, with methods for formatting output.

Structures we will be using are Book and Author:

Now let us see the routers and the route handlers we will be using :

Appended with the dummy entries and port listening will be 8000(we can use another port as well)

Now let us have a look at the functions implemented to achieve the required behaviour:

getBooks()- will return all the book entries present at the moment.

getBook()-will return book entry with the given ID

createBook()- will help us to create a new entry

updateBook()- will help us update a particular entry

deleteBook()-will help us to delete a particular entry with given ID

Now let us see the working of these endpoints using Postman:

GET()- Gives us all the books present at the moment.

POST()-Create a new entry for us and store it.

id will be autogenerated as we defined it in the function.

GET{id}- Give us book with particular id present at the moment.

This is the entry we just created

DELETE{id}- Will delete the book with the particular id given to it.

As we can see the entry we just added is deleted.

PUT()- Will update the entry we have right now:

CONCLUSION-As we can see that using Golang we can easily perform crud operations and test them on Postman to see the results. I hope this article was helpful and thank you taking out time and reading it.

--

--