Build a RESTful CRUD API with Golang Gin and Gorm

Lemoncode21
3 min readJan 15, 2023

--

Building a RESTful CRUD (Create, Read, Update, Delete) API with Golang, Gin, and Gorm is a straightforward process. Gin is a popular web framework for building APIs in Golang, and Gorm is an ORM (Object-Relational Mapping) library for working with databases in Golang. Together, these two libraries make it easy to build a robust and efficient CRUD API.

Article Contents

  • Create database configuration
  • Create Model
  • Create Repository
  • Create Service
  • Create Controller
  • Create Router

First, you’ll need to set up a new project and install the necessary dependencies. You can use the “go mod init” command to initialize a new Go module, and then use “go get” to install Gin and Gorm.

go get -u github.com/gin-gonic/gin

go get -u gorm.io/gorm

After importing the necessary packages, you can create a new Gin application by creating a new gin.Engine variable:

The gin.Default() function creates a new Gin engine with some default middleware already in place, such as the logger and recovery middleware.

Create database configuration

Next, you’ll need to define your connection database. Add the following code to connect the app to the Postgres server. Before that Run this command to install the GORM library and the Postgres driver.

go get gorm.io/driver/postgres

Now create a config/database.go file :

Create model

Next, you’ll need to define your database models. Gorm uses structs to represent database tables, so you’ll need to define a struct for each table you want to work with. For example, if you’re building an API to manage a list of tags, you might define a “Tags” struct like this:

Create repository

Next, Step we create repository package. This package contains the classes that handle data access. There are two files that we will create. The first is related to the interface of the CRUD function with the following code:

The second implementation files of the CRUD interface with the following code:

Create Service

After We created repository, next we create service package. This package contains the classes that handle business logic. Just like the repository in this package we have two files, the first is the service interface file with the following code:

The second implementation files of the service interface with the following code:

Create controller

Next, we will create controller package for tags. This package contains the classes that handle HTTP requests and responses. Create atag_controller.go file and add the following code:

Create router

Now, we create router package. the router package is responsible for defining the routing system. It provides the functionality for matching incoming requests to their corresponding handlers and executing the handlers. Create a router.go file and add following code :

the last step is to change the main function as below:

Finally we have finished creating the code. To run it simply by doing the following command:

go run .\main.go

Conclusion

In conclusion, building a RESTful CRUD API with the Gin and Gorm frameworks in Golang is a straightforward process. The frameworks provide a simple and consistent way to handle routing, middleware, and database interactions, allowing developers to focus on writing business logic. With their high-performance, easy-to-use, and flexible nature, the Gin and Gorm frameworks are a solid choice for building RESTful APIs in Golang.

You can find the complete source code for this tutorial on Github and this Video.

--

--

Lemoncode21
Lemoncode21

No responses yet