英文:
How to merge 2 structs
问题
我正在尝试修改Go语言中的一些内容。我创建了以下结构体:
type MyStruct struct {
DID string `bson:"d_id" json:"d_id"`
PID string `bson:"p_id" json:"p_id"`
...
}
在代码中的某个调用中,我想要向该结构体添加一个新的键,像这样:
type MyNewStruct struct {
*MyStruct
MyNewKey string `bson:"new_key" json:"new_key"`
}
我有两个变量,MyStructData
和MyNewKeyData
,我想要将这两个数据合并到MyNewStruct
中,使得MyStructData
中的所有内容都在键的根目录下,而MyNewKeyData
中的所有内容都在MyNewKey
键下。
我尝试了以下代码,但是不起作用:
theObjectIWant := MyNewStruct{
MyStructData,
"MyNewKey": MyNewKeyData,
}
请问有什么方法可以实现这个需求吗?
英文:
I am trying to modify some stuff in Go. I have the following struct I created
I have an API call returning something like this
MyStruct struct {
DID string `bson:"d_id" json:"d_id"`
PID string `bson:"p_id" json:"p_id"`
...
}
in one call in the code, I want to append a new key to that struct
like
myNewStruct {
DID string `bson:"d_id" json:"d_id"`
PID string `bson:"p_id" json:"p_id"`
...
MyNewKey string `bson:"new_key" json:"new_key"`
}
The thing is, I want to add a new key, and keep the rest at the root of the object, like the object above. without having to rewrite the full object structure, or doing a for loop of each key.
type MyNewStruct struct {
*MyStruct
MyNewKey MyValueType
}
I have, two variable with the data,
MyStructData
and MyNewKeyData
I want to, but don t know how to merge those two data inside MyNewStruct
so that everything in MyStructData will be at the root of the key, and everything in MyNewKeyData
will be indise the key MyNewKey
I am trying stuff like
theObjectIWant := MyNewStruct {
MyStructData,
"MyNewKey" : MyNewKeyData
}
but doesn't work
答案1
得分: 5
当你在结构体中创建一个匿名成员时,编译器会为该成员生成一个与类型相同的名称。在初始化包含的结构体时,你可以使用这个名称。
这里是一个简化的例子:
type MyStruct struct {
DID string
PID string
}
type MyNewStruct struct {
MyStruct
MyNewKey string
}
ms := MyStruct{
DID: "did",
PID: "pid",
}
m := MyNewStruct{
MyStruct: ms,
MyNewKey: "test",
}
在上面的例子中,MyNewStruct
包含了一个匿名成员 MyStruct
。在初始化 MyNewStruct
时,我们使用了 MyStruct
的名称来指定它的值。
英文:
When you create an anonymous member in a struct, the compiler generates a name for the member that is named the same as the type. You can use this name when initializing the containing struct.
Here is a simplified example
type MyStruct struct {
DID string
PID string
}
type MyNewStruct struct {
MyStruct
MyNewKey string
}
ms := MyStruct{
DID: "did",
PID: "pid",
}
m := MyNewStruct{
MyStruct: ms,
MyNewKey: "test",
}
答案2
得分: 1
我不确定我完全理解你在寻找什么,但这可能会有所帮助。
type MyStruct struct {
DID string
PID string
}
type myNewStruct struct {
MyStruct
newKey string
}
func main() {
s1 := MyStruct{DID: `bson:"d_id" json:"d_id"`}
s2 := myNewStruct{MyStruct: s1, newKey: `bson:"new_key" json:"new_key"`}
fmt.Println(s2.DID)
fmt.Println(s2.newKey)
}
这段代码定义了两个结构体类型:MyStruct
和myNewStruct
。MyStruct
包含了DID
和PID
两个字段,而myNewStruct
则包含了MyStruct
类型的嵌入字段和一个newKey
字段。
在main
函数中,我们创建了一个MyStruct
类型的变量s1
,并为其DID
字段赋值为"bson:\"d_id\" json:\"d_id\""
。然后,我们创建了一个myNewStruct
类型的变量s2
,并为其MyStruct
字段赋值为s1
,newKey
字段赋值为"bson:\"new_key\" json:\"new_key\""
。
最后,我们打印了s2
的DID
和newKey
字段的值。
英文:
I'm not sure I fully understand what your looking for, but this may help.
type MyStruct struct {
DID string
PID string
}
type myNewStruct struct {
MyStruct
newKey string
}
func main() {
s1 := MyStruct{DID: `bson:"d_id" json:"d_id"`}
s2 := myNewStruct{MyStruct: s1, newKey: `bson:"new_key" json:"new_key"`}
fmt.Println(s2.DID)
fmt.Println(s2.newKey)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论