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")
}
Gin is a toolkit that helps you build web servers in Go quickly. It acts like a traffic director, routing incoming requests to the specific code you wrote to handle them. Think of it as a receptionist who knows exactly which department to send every visitor to.