记录更换请求参数不生效的问题

type User struct {
    
    
	Username string `json:"username" `
	Password string `json:"password" `
}

func (u *User) AAAAAA(c *gin.Context) {
    
    
	c.ShouldBindQuery(u)
	response.Response(http.StatusOK, 0, vo, "查询成功", c)
}

连续发起两次请求,第一次带参数,第二次不带参数;如图:
带参数
不带参数的也返回了username;
不带参数
原因是开头定义了全局的User结构体,在func (u *User) AAAAAA(c *gin.Context)中,使用*User,赋值一次后这个变量就被保存下来了,第二次c.ShouldBindQuery(u),绑定不到也不会报错,也不会更改User实例。

解决:将func (u *User) AAAAAA(c *gin.Context)修改为func (u User) AAAAAA(c *gin.Context)即可,尽量少使用全局变量

猜你喜欢

转载自blog.csdn.net/leptobo/article/details/131332394