GIN Introduction

Tín Huỳnh
4 min readFeb 17, 2022
Credit: Google.com

What is Gin?

“GIN is a web framework written in Go (Golang). It features a martini-like API with performance that is up to 40 times faster thanks to httprouter. If you need performance and good productivity, you will love GIN.”

Gin allows you to build web applications and microservices in Go. It contains a set of commonly used functionalities (e.g., routing, middleware support, rendering, etc.) that reduce boilerplate code and make it simpler to build web applications.

In fact, GIN is quite popular and widely used to develop projects. So, we should research and practice it right now.

Installation:

Before install Gin, we should say something about Go Mod.

To manage modules in Go conveniently and easily, from version Go 1.11, Go’s team published Go Modules.

We can use Go Modules to install packages automatically when we execute program.

Why we need go modules? Before Go mod introduced, we have some problems :

  • All Go project in $GOPATH/src directory. This was a big limitation in terms of that it restricted where you can keep your project.
  • No native dependency management support.When we do a go get it will download the required package in the $GOPATH/src directory.
  • All the dependency will be downloaded in the $GOPATH/src directory without versioning.

Let's initialize the go module:

go mod init github.com/tinhuynh1/example

Note: You can replace "github.com/tinhuynh1/example” by any string which you want. Above is my style when init go mod with syntax: github.com/{username}/{project_name}

After initializing the go module, project will auto generate 2 files:

go.mod : specifies a module name, dependencies, and the minimal versions.

go.sum : is an auto-generated dependencies lock file.

Both files are located at the root level of the project.

Now, you can install gin, this package will managed by Go Module.

Let’s install gin package:

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

During development, you may sometimes need to update the version of the module you are using to a specific branch or tag version. This can be done through command with the following examples:

Credit: https://blogs.halodoc.io/

Example API use Gin.

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

r := gin.Default() Create a gin router with default middleware.

r.GET(“/ping”, func(c *gin.Context) {...}) In this, you can define and handle your api.

gin.H is a shortcut for Map[string]interface{}

r.run() listen and serve on port 8080, this is Gin's default port number . You can use another by injecting a port number into “run()” method. Example: r.run(“:3000”) .

This is a very simple API. You will receive a JSON string with content “message” : “pong” when you send request http://localhost:8080/ping .

API using GIN

router.GET("/post/:id", func(c *gin.Context) {
id := c.Param("id")
c.JSON(http.StatusOK, gin.H{
"message" : "This post has id is: " + id,
})
})

GET : /post/:id

This code block, I create an API use GET method. Gin Context support get value of param through Param() method. In this case, we need get value of param, which has name is “id". Example, if we browsing http://localhost:8080/post/1 . We will have value of id is 1.

router.POST("/post", func(c *gin.Context) {
//Handle create new post
c.JSON(http.StatusOK, gin.H{
"message" : "Create new post successfully!",
})
})

POST : /post

router.PUT("/post/update/:id", func(c *gin.Context) {
id := c.Param("id")
//Handle update Post
c.JSON(http.StatusOK, gin.H{
"message" : "Update data successfully",
})
})

PUT : /post/:id

router.PUT("/post/update/:id", func(c *gin.Context) {
id := c.Param("id")
//Handle update Post
c.JSON(http.StatusOK, gin.H{
"message" : "Delete data successfully",
})
})

DELETE :/post/:id

Grouping routes

There are simple example API using http method (GET, POST,PUT,DELETE). You can see these routes have the same “/post" in endpoint. And the different is http method. Now, router:= Router.Group(“/post”) creates a route group. Which means, all our requests starting with /post will be routed/handled from this part.

If we group rotes, we will have code like below:

func main() {
router := gin.Default()
api := router.Group("/post")
{
api.GET("/get/:id", func(c *gin.Context) {
id := c.Param("id")
//Handle get post, which you want
c.JSON(http.StatusOK, gin.H{
"message" : "This post has id is: " + id,
})
})
api.POST("/create", func(c *gin.Context) {
//Handle create new post
c.JSON(http.StatusOK, gin.H{
"message" : "Create new post successfully!",
})
})
api.PUT("/update/:id", func(c *gin.Context) {
id := c.Param("id")
//Handle update post,
c.JSON(http.StatusOK, gin.H{
"message" : "Update post id: " + id + " successfully!",
})
})

api.DELETE("/delete/:id", func(c *gin.Context) {
id := c.Param("id")
//Handle delete post
c.JSON(http.StatusOK, gin.H{
"message" : "Delete post id: " + id + " successfully!",
})
})
}
router.Run(":8080")
}

Following above code and use post man to test API. We have the result:

Terminal

This is article about gin basic and simple API with Gin. We don’t have database in this project. So, in the next post, I will write about “How to connect database?And CRUD API with Gin.”.

I hope this article will be useful to you if you are looking for a web framework written in Golang. Thank for reading.

Reference: https://github.com/gin-gonic/gin#quick-start

--

--