如何在Golang中访问结构体中的特定字段

huangapple go评论76阅读模式
英文:

How to access specific fields from structs in Golang

问题

假设我有以下代码:

type User struct {
    ID              string
    Username        string
    Name            string
    Password        string
}

我想做的是创建另一个结构体,可以访问User结构体的某些字段,而不是访问全部字段,以防止人们看到密码,例如。

这样是不起作用的:

type Note struct {
    ID         string
    Text       string
    UserID     User.ID          
}

有没有办法做到这一点,或者我只需创建Note.UserID字段,使其与User结构体中的ID具有相同的数据类型?

英文:

Suppose I have the following code:

type User struct {
    ID              string
    Username        string
    Name            string
    Password        string
}

What I want to do is create another struct that can access certain fields from the User struct, instead of accessing all of it, to prevent people from seeing the password, for example.
This does not work:

type Note struct {
    ID         string
    Text       string
    UserID     User.ID          
}

Is there any way to do this, or do I simply create the Note.UserID field to have the same data type as the ID in the User struct?

答案1

得分: 5

假设这些类型位于不同的包中,你可以通过导出或不导出字段来实现这一点。以小写字母开头的字段不会被导出,意味着它在声明/定义它的包之外是不可见的。所以,在这种情况下,如果用户存在于一个包中,称为user,而另一个类型在另一个包中声明,你可以通过改变定义来实现对属性的“隐藏”:

type User struct {
    ID              string
    username        string
    name            string
    password        string
}

如果这两个类型位于同一个包中,就没有办法使字段私有/隐藏等,所有内容都将在该作用域中可用。

英文:

Assuming the types are in different packages you can do this by exporting vs not exporting the fields. A field who's name begins with a lower case letter is not exported, meaning it is not visible outside the package where it is declared/defined. So, in this case if the user existed in one package, call it user while the other type were declared in another you could accomplish this 'hiding' of properties by changing the definition to;

type User struct {
    ID              string
    username        string
    name            string
    password        string
}

If the two types live in the same package there is no way of making a field private/hidden/ect, everything will be available in that scope.

答案2

得分: 4

《Go编程语言规范》

导出的标识符

为了允许从另一个包中访问标识符,可以将其导出。如果满足以下两个条件,则标识符被导出:

  • 标识符名称的第一个字符是一个Unicode大写字母(Unicode类别"Lu");
  • 标识符在包块中声明,或者是字段名或方法名。

所有其他标识符都不会被导出。

User类型分配一个自己的包,并且不要导出密码。

例如:

package user

type User struct {
    ID       string
    Username string
    Name     string
    password string
}

func (u *User) IsPassword(password string) bool {
    return password == u.password
}
英文:

> The Go Programming Language Specification
>
> Exported identifiers
>
> An identifier may be exported to permit access to it from another
> package. An identifier is exported if both:
>
> the first character of the identifier's name is a Unicode upper case
> letter (Unicode class "Lu"); and
>
> the identifier is declared in the package block or it is a field name
> or method name.
>
> All other identifiers are not exported.

Give User its own package and don't export the password.

For example,

package user

type User struct {
	ID       string
	Username string
	Name     string
	password string
}

func (u *User) IsPassword(password string) bool {
	return password == u.password
}

huangapple
  • 本文由 发表于 2016年2月17日 06:34:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/35444329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定