Gin 使用示例(二十八):查询字符串参数底层解析


示例代码:

func main() {
    router := gin.Default()
    
    // 查询字符串参数使用已存在的底层请求对象进行解析
    // 示例请求 URL:  /welcome?firstname=Jane&lastname=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")
}

点赞 取消点赞 收藏 取消收藏

<< 上一篇: Gin 使用示例(二十七):获取查询字符串以及 POST 表单请求数据

>> 下一篇: Gin 使用示例(二十九):重定向