Go gin如何检查参数是否为post
在这段代码中,我怎么知道是不是前端post的Go gin如何检查参数是否为post,go,go-gin,Go,Go Gin,在这段代码中,我怎么知道是不是前端post的状态 由于go的默认值,如果我检查reqData.Status==0,如果前端没有发布它,它将始终返回true,但在我的情况下,0是一个有意义的值,因此我无法通过等于0来检查它 那么我有其他方法来检查吗 PS:我试过了,发现如果我不分配以下内容,gorm将不会更新struct中的字段: type listParams struct { Status int `form:"status"` Keyword string
状态
由于go的默认值,如果我检查reqData.Status==0
,如果前端没有发布它,它将始终返回true,但在我的情况下,0
是一个有意义的值,因此我无法通过等于0
来检查它
那么我有其他方法来检查吗
PS:我试过了,发现如果我不分配以下内容,gorm
将不会更新struct中的字段:
type listParams struct {
Status int `form:"status"`
Keyword string `form:"keyword"`
ShowType int `form:"show_type"`
}
func List(c *gin.Context) {
var ReqData listParams
_ = c.ShouldBind(&ReqData)
// I fetch this by PostForm() to check it empy if it equal to empty string
if stat := c.PostForm("status"); stat == "" {
ReqData.Status = -99
}
// .......
}
最后,
status
不会更新为0
(据我所知,d.status
应该是0
)使用指针类型绕过0默认值:
var d &User{} // User is a definition of user table
d.ID = 1
d.Name = "Joy"
// d.Status = 1 // It has this field, but I dont assign it
db.Model(&User{}).updates(&d)
检查
d.Status
是否为nil
,否则获取相关值我认为这一约束要求使用唯一的公共方式来构造用户
type listParams struct {
Status &int `form:"status"`
Keyword string `form:"keyword"`
ShowType int `form:"show_type"`
}
通过这种方式,您可以确保用户结构仅通过状态默认值Status始终等于-1的NewUser构建
type user struct { Status int Keyword string ShowTypeint}
func NewUser() (*user) { return &user{Status: -1} }