Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check for &{<nil> } in golang

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

type LoginResponse struct {
    Id          interface{} `json:"_id,omitempty" bson:"_id,omitempty"`
    Email       string      `json:"email"`
    Gender      string      `json:"gender"`
    PhoneNumber string      `json:"phonenumber"`
}

>Solution :

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)

    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading