GIN

https://pkg.go.dev/github.com/gin-gonic/gin#section-readme

What is Gin? So basically Gin is a framework which helps to create Web API in a globally recognized standard way which has really good performance compared to the other programming language frameworks.

Quick Start

package main

import (
  "net/http"

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

func main() {
  r := gin.Default()
  r.GET("/ping", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "message": "pong",
    })
  })
  r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

What is Gin Context?

The gin Context is a structure that contains both the http.Request and the http.Response that a normal http.Handler would use, plus some useful methods and shortcuts to manipulate those.

then we return,

context.JSON(http.StatusOK, gin.H{ “message”: “API is up and working fine” })

In the Gin framework H is a shortcut for map[string]interface{} So H means something like this type H map[string]interface{}

That is to say,

Json (http.statusok, gin.h {“message”: “API is up and working fine”})

Equivalent to

Json (http.statusok, map [string] interface {} {“message”: “API is up and working fine”})

Then we add the endpoint to the router, inside the InitRoutes() the function we add publicRoutes.GET(“health”, HealthCheck) so whenever we are going to add a route in Gin we need to define

Tutorial: https://maneeshaindrachapa.medium.com/go-with-gin-83865cd5cd81

Last updated