Go - Simple REST API with gin

Documentation: gin-gonic/gin

Basic use

The basic example (only a GET request) is:

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

We can use a function instead:

package main

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

func getting(c *gin.Context) {
  c.JSON(200, gin.H{
    "message": "pong",
  })
}

func main() {
    router := gin.Default()
    router.GET("/ping", getting)
    // router.POST("/somePost", posting)
    // router.PUT("/somePut", putting)
    // router.DELETE("/someDelete", deleting)
    // router.PATCH("/somePatch", patching)
    // router.HEAD("/someHead", head)
    // router.OPTIONS("/someOptions", options)

    r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    // router.Run(":3000") for another port
}