Go lang Rest API using Gorilla MUX
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.
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.
2). GET:
To get a single book by its id or all books.
3). UPDATE:
To update particular book information by its id.
4). DELETE:
To delete a particular book by its id.
Write Route handlers and endpoints and start listening.
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.
Check for the POST request.
Check for the Get request.
Check for the Update(PUT) request.
Check for Delete request.
This is the 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!