Member-only story
Build API with Golang, GORM and PostgreSQL (Dockerize)
Web Service with Go and Dockerize
5 min readMar 10, 2024
This post will guide to build Dockerize Web Service with Go, Gin, GORM and PostgreSQL
Prerequisite:
- Go (Gin)
- GORM
- Docker
- PostgreSQL
Initialize Go App:
Make sure you have installed Docker on your machine. Then create new folder go_api
and execute this command inside the go_api
folder
// Init Golang Module
go mod init github.com/<YOUR_GITHUB_USERNAME>/go_api
// Get Package Gin and Gorm
go get -u github.com/gin-gonic/gin
go get -u gorm.io/gorm
Step 1 — Create API Check
This step will guide you to create new api to check if the web service run. First create new file inside root folder with name main.go
and follow this code
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run()
}
Save your file and try to run this web server using command go run main.go
on your terminal and try hit localhost:8080/ping
from your Browser or App…