Go lang Rest API using Gorilla MUX

Sunny Radadiya
3 min readMay 29, 2019

--

Golang

In this tutorial, We learn Rest API using Golang and Gorilla Mux.

Install Gorilla Mux.

$ go get github.com/gorilla/mux

Let’s make a structure for Book.

Here we import all the library and make a structure of Book with author information.

After making a structure, I used a slice of books. which will handle all dynamic operation to shrink, increase and append book.

Structure of Book.

Let’s write all CRUD functions in Golang.

1). POST:

To create a new book and append into books slice. here, I used random id for the book.

Add a new Book.

2). GET:

To get a single book by its id or all books.

Get Single book or all books.

3). UPDATE:

To update particular book information by its id.

Update a book.

4). DELETE:

To delete a particular book by its id.

Delete a book.

Write Route handlers and endpoints and start listening.

Route handlers and endpoints.

Let’s Test this code using POSTMAN

First, write in your terminal below command to run the server.

go run main.go

One more thing for your help, this is a sample for book structure for the postman.

Sample

Check for the POST request.

POST request in POSTMAN

Check for the Get request.

GET request for POSTMAN

Check for the Update(PUT) request.

UPDATE(PUT) request for POSTMAN

Check for Delete request.

Delete request for POSTMAN

This is the Full Implementation.

Full Implementation

Thank you for your time!!

I hope you found this article helpful and get a basic understanding of REST API using Golang and Gorilla-Mux.

That’s it, folks. Thanks for reading. If you liked what you read, don’t forget to clap! Happy coding!

Check out my other article on Golang.

https://medium.com/@radadiyasunny970/a-simple-performance-test-and-difference-go-v-s-java-e6f29ad65293

--

--

Sunny Radadiya

Blockchain Enthusiast | Ethereum Developer | Go Developer | Ethereum Sharding