Basic CRUD in Golang, Learned From Unicorn’s Engineer

Taufan Fadhilah Iskandar
Geek Culture
Published in
6 min readMay 17, 2021
just a cute illustration

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.

Installation

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

command to init mod Golang project

After that, we need to install Gin Gonic which make our Golang can be accessed using request. Just type this command in terminal

command to install gin-gonic libary

Last step of installation is we need to create a main.go file. Inside that file, call gin-gonic and create a router

main.go

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.

task’s table schema

Next, we will make some files and folders to maintain our code like this

folder structure

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.

entity.go

second file we need to define input as struct inside input.go.

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.

repository.go

next file is service.go, this file purposes to call the repository functions and functions inside service file might be called to other domain.

service.go

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.

task.go

Last step of this section are define our database connection, do auto migration, define repository, service, and handler and then create routing.

main.go

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().

repository.go

To call the function in Repository, we will call it in service.go file. Let’s create a new function with name Index().

service.go

Next, call service’s function inside task.go in handler. Let’s create new function with name Index.

task.go

Last touch is, we just need to register new route in main.go.

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.

input.go

Next, of course we need to create a new function inside repository.go. Just call SelectById.

repository.go

Call the new function in service.go. Create new function with name SelectById.

service.go

Call new service’s function in handler, so we can create a new route. Let’s make it with name Show.

task.go

Last touch is make a new route in main.go so we can get record by ID.

main.go

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.

repository.go

Move to service.go file, create a new function with name Update as well.

service.go

And then create a new function in our handler to allow route updating the data.

task.go

Last touch, register new route in main.go.

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.

repository.go

also in service.go, create a new function with name Destroy.

service.go

inside handler file, create a new function as well called Destroy.

service.go

Last touch is registering a new route as usual in main.go.

main.go

Restart your server and hit the new API we just created.

ganbatte!!

Conclusion

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.

I have provide my Github if you want to clone the project. Please feel free to message me maybe you have question or wanna do a collaboration, just go through my LinkedIn. Thanks!

--

--