ShouldBindQuery获取GET参数默认值

作者:matrix 被围观: 733 次 发布时间:2023-10-31 分类:Golang | 一条评论 »

图片5702-ShouldBindQuery读取GET参数默认值

验证器结构体的form标签中设置default即可,仅测试ShouldBindQuery有效读取,其他绑定方法未知

验证器结构体

type UserListValidator struct {
    Type     string `form:"type,default=RECOMMEND" binding:"omitempty,oneof=NEAR RECOMMEND" label:"列表类型"` //列表类型 NEAR:附近(默认) RECOMMEND:推荐
}

控制器方法

func (u *UserController) List(ctx *gin.Context) {
    validator := validators.UserListValidator{}
    if err := ctx.ShouldBindQuery(&validator); err != nil {
        u.JSONResponseError(ctx, err)
        return
    }
    //validator.Type 

参考:

https://github.com/gin-gonic/gin/issues/1052#issuecomment-1609678741

https://www.vksir.zone/posts/go_struct_default/

其他文章:
本文固定链接:https://www.hhtjim.com/shouldbindquery-gets-the-default-value-of-the-get-parameter.html
matrix
本文章由 matrix 于2023年10月31日发布在Golang分类下,目前没有通告,你可以至底部留下评论。
转载请注明:ShouldBindQuery获取GET参数默认值-HHTjim'S 部落格
关键字:

有1条评论 »

  1. 勿忘初心 勿忘初心 2023-11-24 10:28:37 +0800#1

    您好,看你的站做的挺不错的,有没有出手的打算,想出手的话,联系QQ1587894193。

添加新评论 »

 🙈 😱 😂 😛 😭 😳 😀 😆 👿 😉 😯 😮 😕 😎 😐 😥 😡 😈 💡

插入图片

NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!