无法将比较结构转换为nil

时间:2018-12-24 06:22:07

标签: go

我有此代码:

    var user person.Model;
    json.NewDecoder(r.Body).Decode(&user)

    if user.Handle == "" || user.Email == "" {
        panic(utils.AppError{
                StatusCode: 409,
                Message:    "Missing handle/email property in request body.",
        })
    }

    if v.PeopleByHandle[user.Handle] != nil {

    }

person.Model是一个结构,但出现此错误:

  

无法将nil转换为更多Model类型

我尝试了一些&和*技巧,但是我无法编译它-有人知道如何正确执行此操作吗?

enter image description here

person.Model结构类似于:

type Model struct {
    ID        int    `json:"id, omitempty"`
    Handle    string `json:"handle, omitempty"`
    Role      string `json:"role, omitempty"`
    Password  string `json:"password, omitempty"`
    Work      string `json:"work, omitempty"`
    Image     string `json:"image, omitempty"`
    Firstname string `json:"firstname, omitempty"`
    Lastname  string `json:"lastname, omitempty"`
    Phone     string `json:"phone, omitempty"`
    Email     string `json:"email, omitempty"`
}

1 个答案:

答案 0 :(得分:1)

您的问题尚不清楚。如果要将结构与nil进行比较,则变量应为指针,因为struct的零值是空结构,而指针的零值为nil。
但是,如果您要检查地图中的键,可以执行以下操作:

if obj,ok := v.PeopleByHandle[user.Handle] ;ok {
// Found
}
相关问题