为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 golang 中检查 &{<nil> }

如何在 golang 中检查 &{<nil> }

Go
凤凰求蛊 2022-10-24 16:01:11
我正在尝试从数据库查询中检查结构是否为零,但它不起作用。 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"`}
查看完整描述

1 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

尝试这样做,希望它有效


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)


    }


查看完整回答
反对 回复 2022-10-24
  • 1 回答
  • 0 关注
  • 82 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信