Skip to content

Typed Parameters #4493

@Jaidenmagnan

Description

@Jaidenmagnan

Feature Description

As of right now when getting a URL paramter in Gin the following must be done to change the type to an Integer.

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

	r.GET("/add/:id", func(c *gin.Context) {
		idStr := c.Param("id")

		idInt, _ := strconv.Atoi(idStr)

		c.JSON(http.StatusOK, gin.H{"id plus one": idInt + 1})
	})

	r.Run()
}

I propose that there is a way to directly query the param as an Integer instead of the strconv.Atoi(idStr) each time. Like the following:

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

	r.GET("/add/:id", func(c *gin.Context) {
        id, err  := c.ParamInt("id") 

        if err != nil {
            return 
        }

		c.JSON(http.StatusOK, gin.H{"id plus one": id + 1})
	})

	r.Run()
}

Should be simple to add.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/proposalGot an idea for a feature that Gin doesn't have currently? Submit your idea here!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions