HTTP GET Routes
Here is how to define a HTTP GET Route in Gin.
Simple Version
func main() {
router := gin.Default()
// This route returns a JSON String with a message key.
router.GET("/hello", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "Hello, World!",
})
})
router.Run(":8080")
}
ℹ️
Make sure to import
net/http
to use http.StatusOK
or you can use an integer like 200 instead.Separate a GET request function body from route declarion using a named function
func helloHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "Hello, World!",
})
}
func main() {
router := gin.Default()
// Register the named handler function with the GET route
router.GET("/hello", helloHandler)
router.Run(":8080")
}
Last updated on