Let's create a simple REST API with Go or Golang to understand CRUD (Create Read Update Delete) operations with the most commonly used HTTP methods; POST, GET, PUT, PATCH, and DELETE.
My team assigned me to create a service that will handle millions of requests. I decided to create it with Go. The most reason is fast. You code this simple language, compile it, then run it. The compiled one is the native program, create a service on it, then run it on the operating system. It has no dependency.
Some high tech companies or startups have some services that run on Go. It is worth to learn. This tutorial is to make you a little familiar with Go. What will you create is a simple REST API for articles or a blog. Here is my updated article about a simple explanation of REST API. So, it is a nice start if you are new to Go.
First, you need to install Go on your system. You can go to https://golang.org/doc/install then follow the instructions. The last is the absolutely text editor. I was using VS Code while writing this tutorial.
First, you need to write your package. It is the same as the main function name, main(), and the file name, main.go. You don't need to write the packages that you need early because VS Code can do that on the fly. It can suggest you related extensions that you need to install. It makes your code easy and simple.
The most crucial fields of the article are the title and description. There is also some defined article. In the real situation, you need to get the data from the database. They are enough for our tutorial. The struct is a collection of fields. I chose the map, key-value data structure, to easily access and define the data in constant time.
Just print all articles as JSON.
Parsing JSON request to the article struct, add it to the list of articles, then print them as JSON. The log.Fatal is to print the error and immediately abort the program. The json.Unmarshal parses the request JSON body to the defined struct.
Get the parameter article which is the article ID. Convert it from string to integer. Check wheater the article exists or not. Return 404 if not exist. Print the article if exist.
Here is we replace the entire article with the new one even only some fields updated.
Here is we only update some updated fields of the article.
Delete or remove the article. The status must be OK even if there is no response data.
We route those functions to some HTTP methods even the URL address is the same. That is the REST API technically.
Easy and simple right? You can use go run main.go for every time you have made some changes and go build for the final one. Here is the final code along with a Postman collection ready for you to try, https://github.com/aristorinjuang/go-rest-api. In the end, it brought an experience of Go to you. We had implemented CRUD operations to the REST API and explained the most commonly used HTTP methods of the REST API, especially PUT vs PATCH.