Build Simple REST API using Go with gin-gorm

Awang Trisakti
3 min readMar 20, 2023
Photo by Shahadat Rahman on Unsplash

REST (Representational State Transfer) is an architectural method of communication that uses HTTP protocol for data exchange. Application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. In short, API can make other applications, whether it’s a mobile apps or something else, can communicate with our application.

In this post I want to share how to create a simple REST API in Go using gin and gorm in a simple way.

First, create new go project using go mod command

# Create folder
mkdir go-api
# Change dir into go-api
cd go-api
# Init go project
go mod init go-api

Install gin package

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

Install gorm package and db driver, db driver can be customized as needed or desired. For supported db driver you can check in here. In this post I will use postgreSQL.

go get -u gorm.io/gorm
go get -u gorm.io/driver/postgres

Since Go let us structure the project according to what we want and also go does not have a standard structure. This is structure I will use for this project or you can define your own.

Create a go file for set db connection in, I will create it in src/config
as db.go.

package database

import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

func Connect() *gorm.DB {
dsn := "host=localhost user=root password=root dbname=go-api port=9920 sslmode=disable TimeZone=Asia/Shanghai"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}

return db
}

Then create a struct for represent our data from database, in this I will create a simple src/apps/post/model.go

package post

type Post struct {
ID int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}

Then create a controller or handler file src/apps/post/handler.go

Create a go file src/cmd/main/main.go, then add code to setup the router.

package main

import (
"go-api/src/apps/post"
"go-api/src/config/database"

"github.com/gin-gonic/gin"
)

func main() {
// Connect to database
db := database.Connect()

// Migrate database
db.AutoMigrate(&post.Post{})

// Create post handler
postHandler := post.NewPostHandler(db)

// Create router
router := gin.Default()
api := router.Group("/api")
postRoute := api.Group("/post")
postRoute.GET("", postHandler.GetPosts)
postRoute.GET("/:id", postHandler.GetPost)
postRoute.POST("", postHandler.CreatePost)
postRoute.PUT("/:id", postHandler.UpdatePost)
postRoute.DELETE("/:id", postHandler.DeletePost)

// Run server
router.Run()
}

Test
Run go run src/cmd/main/main.go, once our app is running we can test our API using apps such as postman, insomnia etc. I will use postman for testing our API.

List post
Get detail post
Create a post
Update post
Delete post

Thanks for reading my post, please feel free to leave comments for any suggestions or questions. Share this post if you find this post useful. See ya…

Repository
-
https://github.com/awgst/go-api

References
-
https://www.ibm.com/topics/rest-apis#:~:text=the%20next%20step-,What%20is%20a%20REST%20API%3F,representational%20state%20transfer%20architectural%20style.
- https://gorm.io/
- https://gin-gonic.com/

--

--