HTTP Route Parameters

HTTP Route Parameters

Here is how to implement route parameters like /book/:id in Gin

After specifying a router parameter with a named :param in the route,
We can access the parameter using

param_var := c.Param("param")

Full Example

func main() {
	router := gin.Default()

    router.GET("/book/:id", func(c *gin.Context) {
        
        id := c.Param("id")

        c.JSON(http.StatusOK, gin.H{
            "message": "Book's id is, " + id + "!",
        })
    })

	router.Run(":8080")
}
Last updated on