嵌套结构在aerospike-go库中的意外行为

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

Embedded structs in aerospike-go library unexpected behaviour

问题

Aerospike Go客户端

问题

需要将一组常见字段添加到所有集合中,例如CreatedAt、UpdatedAt、DeletedAt等。为此,我创建了一个结构体,并将其嵌入到所有集合的结构体中。我希望将公共结构的字段保存为给定记录的字段。

示例代码

type Table struct {
   CreatedAt time.Time
   UpdatedAt time.Time
   DeletedAt time.Time
}


type Account struct {
   Table
   Name string
   Status bool
   .....
}

对于上述提到的结构体Account,我希望记录存储的bin名称为

CreatedAt,UpdatedAt,DeletedAt,Name,Status.....

但是当记录被存储时,bin名称为

Table,Name,Status...

其中Table将是一个具有键值的映射。

是否可能实现预期的行为?如果可以,如何实现?

英文:

Aerospike Go client

Problem

Need to add set of common fields to all the sets ,ie CreatedAt,UpdtedAt,DeletedAt etc. For the same I have created a struct and embed that with all the set structs. I need the Fields of the common structure saved in the set as fields of the given record

Sample Code

type Table struct {
   CreatedAt time.Time
   UpdatedAt time.Time
   DeletedAt time.Time
}


type Account struct {
   Table
   Name string
   Status bool
   .....
}

For the above mentioned struct Account.I expect the record stored with bin names

CreatedAt,UpdatedAt,DeletedAt,Name,Status.....

But when the records are stored bin names are

Table,Name,Status...

Where Table would be a map with key values

Is it possible to achieve the expected behaviour ? if so how?

答案1

得分: 1

结构嵌入嵌入方法而不反映属性。内部类型的属性可以通过外部类型访问,但不会出现在外部类型的属性中。因此,当定义类型为Table的属性时,您实际上定义了类型为Table的属性,而不是将Table类型的所有属性反映到Account类型中。

尝试打印您的Account结构体 - playground

英文:

Struct embedding embeds methods and doesn't reflect attributes. Attributes of inner type are accessible thorough outer type but don't exist among attributes of outer type. So defining attribute of type Table you literally define attribute of type Table not reflect all attributes of type Table to type Account.

Try to print your Account struct - playground.

huangapple
  • 本文由 发表于 2017年1月30日 15:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/41931030.html
匿名

发表评论

匿名网友

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

确定