func handler(c *gin.Context) { username := c.Param("username") c.JSON(200, gin.H{"message": "Hello " + username}) }
func handler(c *gin.Context) { id := c.Param("id") if _, err := strconv.Atoi(id); err != nil { c.JSON(400, gin.H{"error": "ID must be an integer"}) return } // continue processing request }In this example, the value of the "id" parameter is retrieved using the Param function, and then checked to ensure that it is an integer. If it is not, the handler returns a 400 error response. Overall, the Context Param function is a useful tool for retrieving parameters from the URL in gin-gonic/gin handlers, allowing for greater flexibility and validation of user input.