How to build REST API with Gin

Build a REST API with Gin by initializing a module, defining routes with handlers, and running the server.

Initialize a Go module, install the Gin framework, create a router, define a handler function, and start the server on port 8080.

package main

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

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{"message": "Hello World"})
	})
	r.Run(":8080")
}