查询URL中字符串参数

查询URL中字符串参数

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

    // 访问路径:/welcome?firstname=Jane&lastname=Doe 输出 "Hello Jane Doe"
    router.GET("/welcome", func(c *gin.Context) {
        firstname := c.DefaultQuery("firstname", "Guest") // 默认参数
        lastname := c.Query("lastname") // 同等 c.Request.URL.Query().Get("lastname")

        c.String(http.StatusOK, "Hello %s %s", firstname, lastname)
    })
    router.Run(":8080")
}

Last updated

Was this helpful?