英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论