Golang与结构混淆

  type Person struct {
ID intjson:“ id”
}
\ ntype PersonInfo [] Person

type PersonInfo2 [] struct {
ID intjson:“ id”
}
</ code> </ pre>

PersonInfo和PersonInfo2 </ p>
</ div>

展开原文

原文

type Person struct {
    ID int `json:"id"`
}

type PersonInfo []Person

type PersonInfo2 [] struct {
   ID int `json:"id"`
}

Is there have difference between PersonInfo and PersonInfo2

3个回答



主要区别在于您必须在PersonInfo / PersonInfo2初始化之外初始化Person对象。 由于PersonInfo2是匿名结构类型的数组,因此在PersonInfo2初始化之外,我们对该类型一无所知。 </ p>

所以它们都可以像这样初始化:</ p>

  m:= PersonInfo {{1},{2}} 
n: = PersonInfo2 {{1},{2}}
</ code> </ pre>

但是,如果要附加匿名结构类型的元素,则必须指定完整类型: </ p>

  append(n,struct {ID intjson:“ id”} {3})
</ code> </ pre>

如果我们将它们打印出来,我们可以看到它们看起来是相同的:
fmt.Printf(“%+ v
%+ v”,m,n)</ code>
输出:</ p >

  [{ID:1} {ID:2}] 
[{ID:1} {ID:2}]
</ code> </ pre>
\ n

但是,由于PersonInfo是Person类型的数组,而PersonInfo2是匿名结构类型的数组,因此它们不会完全相等。 因此,如下所示:</ p>

 如果!reflect.DeepEqual(m,n){
print(“ Not Equal”)
}
</ code> </ pre >

将显示“不等于”。</ p>

这里是一个供您自己查看的链接。</ p>

在附加到PersonInfo2时,我们必须为要附加的每个值重复匿名结构类型,这可能更好 将PersonInfo用作Person类型的数组。</ p>
</ div>

展开原文

原文

The main difference is how you have to initialize Person objects outside of the PersonInfo/PersonInfo2 initialization. Since PersonInfo2 is an array of the anonymous struct type we know nothing about this type outside of the PersonInfo2 initialization.

So they can both be initialized like this:

m := PersonInfo{{1}, {2}}
n := PersonInfo2{{1},{2}}

However if we want to append an element of the anonymous struct type we would have to specify the full type:

append(n, struct { ID int  `json:"id"` }{3})

If we print these out we can see they appear to be the same: fmt.Printf("%+v %+v", m, n) Outputs:

[{ID:1} {ID:2}]
[{ID:1} {ID:2}]

However they wont be deeply equal because PersonInfo is an array of type Person and PersonInfo2 is an array of anonymous struct type. So the following:

if !reflect.DeepEqual(m,n) {
    print("Not Equal")
}

will print "Not Equal".

Here is a link to see for yourself.

When appending to PersonInfo2 we have to repeat the anonymous struct type for every value we want to append, it is probably better to use PersonInfo as an array of type Person.

dongle19863
dongle19863 谢谢,我已经相应地更新了答案。
大约 3 年之前 回复
dtt3399
dtt3399 请注意,切片的初始化不是问题,因为您可以省略elemnt的类型,而仅使用{},当您想向切片中添加新元素时,麻烦就来了。 play.golang.org/p/ZQR0zjEFS-
大约 3 年之前 回复



Go正在使用 结构类型</ code> ,这意味着,只要这两种类型具有相同的基础类型,它们就是等效的。 </ p>

因此,对于您的问题: Person </ code>和 struct {ID int'json:“ id”'} </ code>是等效的,因为它们具有 相同的基础类型,因此 PersonInfo </ code>和 PersonInfo2 </ code>也是等效的。 我使用json标签简化了示例。</ p>

  person1:= Person {1} 

pStruct1:= struct {ID int} {2}

pInfo1:= PersonInfo {
person1,
pStruct1,
}

pInfo2:= PersonInfo2 {
person1,
pStruct1,
}

fmt.Printf(“%+ v \ n“,pInfo1)
fmt.Printf(”%+ v“,pInfo2);

//输出
// PersonInfo:[{ID:1} {ID:2}]
// PersonInfo2: [{ID:1} {ID:2}]
</ code> </ pre>

代码示例: https://play.golang.org/p/_wlm_Yfdy2 </ p>
</ div>

展开原文

原文

Go is using structural typing, that means, as long as the two types have the same underlying type, they are equivalent.

So, for your question: Person and struct{ ID int 'json:"id"'} is equivalent because they have the same underlying type, and therefore, PersonInfo and PersonInfo2 are also equivalent. I took of the json tags to simplify the example.

person1 := Person{1}

pStruct1 := struct{ID int}{2}


pInfo1 := PersonInfo{
    person1,
    pStruct1,
}

pInfo2 := PersonInfo2{
    person1,
    pStruct1,
}


fmt.Printf("%+v
", pInfo1)
fmt.Printf("%+v", pInfo2);

//outputs
//PersonInfo: [{ID:1} {ID:2}]
//PersonInfo2: [{ID:1} {ID:2}]

Code example: https://play.golang.org/p/_wlm_Yfdy2

dongren1353
dongren1353 谢谢,真的帮了我
大约 3 年之前 回复



类型PersonInfo </ code>是 Person </ code>的对象,而 PersonInfo2 < / code>本身就是 class </ code>(或Golang中的 type </ code>)。 即使它们的数据结构相似。</ p>

因此,当您运行DeepEqual()检查相似性时,结果将是错误的。</ p>

前面的注释示例:</ p>

 如果!reflect.DeepEqual(m,n){
print(“不等于”)
}
</ code> </ pre>

希望这会有所帮助。</ p>
</ div>

展开原文

原文

type PersonInfo is an object of Person, while PersonInfo2 is a class (or type in Golang) by itself. Even though their data structure is similar.

So, when you run DeepEqual() to check the similarity it will turn out to be false.

Example by previous comment:

if !reflect.DeepEqual(m,n) {
    print("Not Equal")
}

Hope this helps.

立即提问
相关内容推荐