简体   繁体   English

Go-访问指针结构的字段

[英]Go - Accessing fields of a pointer struct

I have a User struct containing sensitive fields like password and email. 我有一个用户结构,其中包含敏感字段,例如密码和电子邮件。 For a public instance of User, for example a public RSVP on an event page, I want to exclude sensitive fields from appearing in my JSON output, even if they're blank. 对于User的公共实例,例如事件页面上的公共RSVP,我希望排除敏感字段以使其不显示在JSON输出中,即使它们为空。

Based on this article , I'm using a composite struct to mask undesired fields. 根据本文 ,我正在使用复合结构掩盖不需要的字段。

QUESTION: during rows.Scan in my database func, how do I properly access the fields of the pointer struct within the composite struct? 问题:在数据库功能中的rows.Scan期间,如何正确访问复合结构中指针结构的字段? I'm getting panic errors thrown, because fields are not being found. 由于找不到字段,因此引发了恐慌错误。

My regular User struct: 我的常规用户结构:

type User struct {
    ID          int `json:"id"`
    FirstName   string `json:"firstname"`
    LastName    string `json:"lastname"`
    Registered  int `json:"registered"`
    Email       string `json:"email"`
    Password    string `json:"password"`
    ProfilePic  string `json:"profilepic"`
}

The new additional structs based on the method in the article linked above: 根据上面链接的文章中的方法,新的其他结构:

type omit *struct {

}


type PublicUser struct {
    *User
    Registered omit `json:”registered,omitempty"`
    Email omit `json:”email,omitempty"`
    Password omit `json:"password,omitempty"`
}

My database func, where an error is occuring: 我的数据库函数,发生错误:

func getUsers(db *sql.DB) ([]PublicUser, error) {

    query := `SELECT users.id, users.name_first, users.name_last
    FROM users
    ORDER BY users.name_first asc`

    users := []PublicUser{}

    rows, err := db.Query(query, e.ID)
    if err != nil {
        return nil, err
    }
    defer rows.Close()

    for rows.Next() {

        var user PublicUser


        // ERROR OCCURS HERE. Seems like these fields cannot be found in PublicUser.
        // Is the pointer to User working within my PublicUser struct?

        err := rows.Scan(&user.ID, &user.FirstName, &user.LastName)



        if err != nil {
            return nil, err
        }

        users = append(users, user)
    }

    return users, nil

}

My original JSON output, not using the articles' method; 我的原始JSON输出,未使用文章的方法; only using my regular User struct: 仅使用我的常规User结构:

[{

    "ID": 25,
    "FirstName": "Jim",
    "LastName": "Brown",
    "Registered": 0,
    "Email": "",
    "Password": "",
    "ProfilePic": ""

},
…
]

Desired JSON output: 所需的JSON输出:

[{

    "ID": 25,
    "FirstName": "Jim",
    "LastName": "Brown",
    "ProfilePic": ""

},
…
]

Problem there is that when you initialize the variable here: 问题在于,当您在此处初始化变量时:

var user PublicUser

All the fields for user take their "zero" values. user所有字段均采用其“零”值。

Since you are embedding a pointer, and zero value for pointers is nil , you can't use that pointer without getting an error. 由于您要嵌入一个指针,并且指针的零值为nil ,因此不能在没有错误的情况下使用该指针。

In order for this to work, you should initialize user like this: 为了使它起作用,您应该像这样初始化user

user = PublicUser{ User: &User{} }

(or don't declare it as a pointer) (或者不要将其声明为指针)

See a playground here showing the issue and then initializing the variable as described above for it to work. 请参见此处显示问题的操场,然后按照上面的描述初始化变量以使其起作用。

https://play.golang.org/p/fXwvATUm_L https://play.golang.org/p/fXwvATUm_L

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM