How to check for &{<nil> } in golang [duplicate]
I am trying to check whether the struct is nil or not from the db query but its not working.
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)
}
Both the conditions are not working .Please help
Login response
type LoginResponse struct {
Id interface{} `json:"_id,omitempty" bson:"_id,omitempty"`
Email string `json:"email"`
Gender string `json:"gender"`
PhoneNumber string `json:"phonenumber"`
}
Solution 1:
Try doing this ,hopefully it works
if (LoginResponse{}) != *loginResponse { // as your getting address so derferencing the pointer to struct
response := models.Response{
Message: "Login Success",
Success: true,
Output: token,
}
return c.JSON(response)
}