【发布时间】:2022-01-12 14:50:49
【问题描述】:
我正在尝试从 db 查询中检查结构是否为 nil,但它不起作用。
fmt.Println(loginResponse) //&{<nil> }
if !reflect.ValueOf(loginResponse).IsNil() { // Its not working
response := models.Response{
Message: "Login Success",
Success: true,
Output: token,
}
return c.JSON(response)
}
if !reflect.ValueOf(loginResponse).IsZero() { // its also not working
response := models.Response{
Message: "Login Success",
Success: true,
Output: token,
}
return c.JSON(response)
}
这两个条件都不行。请帮忙
登录响应
type LoginResponse struct {
Id interface{} `json:"_id,omitempty" bson:"_id,omitempty"`
Email string `json:"email"`
Gender string `json:"gender"`
PhoneNumber string `json:"phonenumber"`
}
【问题讨论】:
-
显示
loginResponse是什么? -
如果你传递的是一个指向结构体的指针,它包含一个 nil 指针 (
&something{nil}),它与传递一个 nil 指针 (nil) 不同。 -
是的,但是如何检查呢?
-
你在检查结构的零值吗?见How to check for an empty struct?
标签: go