如何合并两个结构体

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

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"`
}

我有两个变量,MyStructDataMyNewKeyData,我想要将这两个数据合并到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)
}

这段代码定义了两个结构体类型:MyStructmyNewStructMyStruct包含了DIDPID两个字段,而myNewStruct则包含了MyStruct类型的嵌入字段和一个newKey字段。

main函数中,我们创建了一个MyStruct类型的变量s1,并为其DID字段赋值为"bson:\"d_id\" json:\"d_id\""。然后,我们创建了一个myNewStruct类型的变量s2,并为其MyStruct字段赋值为s1newKey字段赋值为"bson:\"new_key\" json:\"new_key\""

最后,我们打印了s2DIDnewKey字段的值。

英文:

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)
}

huangapple
  • 本文由 发表于 2019年3月15日 10:01:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/55174547.html
匿名

发表评论

匿名网友

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

确定