I have been working for many projects and for backend tech i was used PHP for almost 10 years, and this last 3 years I also trying Node JS as one of my backend stack. And now i want to focus using Golang since Golang have a good track record and many startups are using this as their backend stack.
To help me going deeper in Golang, i decided to bought a Golang course and luckily my mentor is an engineer in one of unicorn in Indonesia. Personally, first time when i created a simple project using Golang i felt this stack is so strict and complicated. But after knowing the result from memory and speed, i was impressed with this language. So in this article i will write a tutorial to make a CRUD API using Golang, it might be a long tutorial because i want to write step by step.
Firstly, you need to make a empty folder (let’s say the folder name is go-basic-crud) then go to that folder.
next, in terminal you need to set initial mod by using this command
After that, we need to install Gin Gonic which make our Golang can be accessed using request. Just type this command in terminal
Last step of installation is we need to create a main.go file. Inside that file, call gin-gonic and create a router
Finally, we can test our installation by running the project. Use go run main.go to run, and when you access /ping from browser, a json response will be show.
Insert to DB
Before going to code, don’t forget to create a new database (let’s say the name is go-basic). We’re not going to create the table because we will use auto migration to create the tables.
Next, we will make some files and folders to maintain our code like this
First, we make a entity.go inside task folder. This file purposely to define our table schema, so this file will be used to auto migration and ORM.
second file we need to define input as struct inside input.go.
third file we need to make is repository.go which be used to do manipulation our database records. Don’t forget to install GORM first.
next file is service.go, this file purposes to call the repository functions and functions inside service file might be called to other domain.
last file is task.go inside handler folder. This file purposes to let router call the function and using service functions, it’s kinda of controller file.
Last step of this section are define our database connection, do auto migration, define repository, service, and handler and then create routing.
after restarting our server, you can try to hit /task to get all records from task table.
Get All Data from DB
Actually we already do a long step for storing data, now we will continue to get the data.
First, we need to create a new function in repository.go to get all data from DB. Let’s give the function name SelectAll().
To call the function in Repository, we will call it in service.go file. Let’s create a new function with name Index().
Next, call service’s function inside task.go in handler. Let’s create new function with name Index.
Last touch is, we just need to register new route in main.go.
Get Data by ID
For creating this flow, is not really much change we will do. So let’s start.
First, we need a new struct in input.go to get id from url. Just call InputTaskDetail struct.
Next, of course we need to create a new function inside repository.go. Just call SelectById.
Call the new function in service.go. Create new function with name SelectById.
Call new service’s function in handler, so we can create a new route. Let’s make it with name Show.
Last touch is make a new route in main.go so we can get record by ID.
Restart your server, and access by using /task/:id (e.g localhost:8080/api/task/1) to get the data.
Update the Data
Next section is updating the data because it’s related with select data by ID.
Create a new function in repository.go with name Update.
Move to service.go file, create a new function with name Update as well.
And then create a new function in our handler to allow route updating the data.
Last touch, register new route in main.go.
To test our new route, don’t forget to restart the server first.
Delete Data by ID
actually the step is not really different with updating data, just create a new function in repository, service, handler and register the new route.
In repository.go, create a new function with name Destroy.
also in service.go, create a new function with name Destroy.
inside handler file, create a new function as well called Destroy.
Last touch is registering a new route as usual in main.go.
Restart your server and hit the new API we just created.
For me who first time create CRUD API with Golang was felt so strict and complicated, we need to create condition to handle err. But this language is very good, we can do more logic and concurrency requests at the same time. And also we can use compiled project in any OS.