Golang anonymous struct in initializing slice of pointers

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

Golang anonymous struct in initializing slice of pointers

问题

从《GOPL》第7章(第7.6节)中,我注意到了这一行代码:

var tracks = []*Track{
     {"Go", "Delilah", "From the Roots Up", 2012, length("3m38s")},
     {"Go", "Moby", "Moby", 1992, length("3m37s")},
     {"Go Ahead", "Alicia Keys", "As I Am", 2007, length("4m36s")},
     {"Ready 2 Go", "Martin Solveig", "Smash", 2011, length("4m24s")},
}

我对如何初始化 Track 指针的切片感到困惑。
所以后来我尝试了以下示例:

type Ex struct {
    A, B int
}

a := []Ex{Ex{1, 2}, Ex{3, 4}}
b := []Ex{{1, 2}, {3, 4}}
c := []*Ex{&Ex{1, 2}, &Ex{3, 4}}
d := []*Ex{{1, 2}, {3, 4}}
e := []*Ex{{1, 2}, &Ex{3, 4}}

我发现这5种情况都没有错误。{1, 2} 似乎是一个名为 "匿名结构体" 的结构体,但它让我感到困惑,因为即使我尝试填充 *Ex 指针而不是 Ex 结构体,它也能正常工作。

此外,当我尝试以下代码时,它会报语法错误:

f := []*Ex{&{1, 2}, &{3, 4}} // 语法错误!

有人可以帮忙解释一下这些情况实际上是怎么回事吗?

英文:

From Chapter 7 of GOPL (Section 7.6), I noticed this line:

var tracks = []*Track{
     {"Go", "Delilah", "From the Roots Up", 2012, length("3m38s")},
     {"Go", "Moby", "Moby", 1992, length("3m37s")},
     {"Go Ahead", "Alicia Keys", "As I Am", 2007, length("4m36s")},
     {"Ready 2 Go", "Martin Solveig", "Smash", 2011, length("4m24s")},
}

I was kind of confused by how it initialized the slice of Track pointers.
So later I tried the following example:

type Ex struct {
    A, B int
}

a := []Ex{Ex{1, 2}, Ex{3, 4}}
b := []Ex{{1, 2}, {3, 4}}
c := []*Ex{&Ex{1, 2}, &Ex{3, 4}}
d := []*Ex{{1, 2}, {3, 4}}
e := []*Ex{{1, 2}, &Ex{3, 4}}

I found all of the 5 cases are okay with no errors. The {1, 2} seems to be a structure called "anonymous struct", but it confuses me since it works fine even when I am trying to fill in *Ex pointers instead of Ex struct.

Furthermore, when I try the following code, it complains syntax error:

f := []*Ex{&{1, 2}, &{3, 4}} // Syntax Error!

Can somebody help explain what is actually going on in these cases?

答案1

得分: 6

我认为"anonymous"在这里并不是完全正确的词。这个结构体有一个名字,"Track"或者"Ex"。但是你用一种简化的方式进行了初始化:

f := []<type>{{...}, {...}}

大致上等同于:

f := []<type>{<type>{...}, <type>{...}}

但是它比简单的字符串替换要聪明一些。如果<type>是一个指针类型,比如*Ex或者*Track,它也会自动正确地进行初始化:

f := []*<type>{{...}, {...}} // 等同于...
f := []*<type>{&<type>{...}, &<type>{...}}

对于映射类型也是一样的:

f := map[string]*<type>{"a": {...}, "b": {...}} // 等同于...
f := map[string]*<type>{"a": &<type>{...}, "b": &<type>{...}}

进一步澄清一下,匿名结构体是指没有单独的类型定义的结构体。这些是匿名类型,但不是匿名结构体。匿名结构体是一个没有关联类型定义的结构体。初始化值时不需要引用类型的语法对于使其易于管理是至关重要的:

f := []struct{
    A, B int
}{
    {1, 2}, {3, 4}
}
// 或者使用指针...
f := []*struct{
    A, B int
}{
    {1, 2}, {3, 4}
}

在列表中的结构体没有类型定义,因此它是匿名的。由于初始化的简化语法,这是可以的 - 我不需要一个名字来初始化它。

英文:

I think anonymous isn't quite the correct word here. The struct has a name, "Track" or "Ex". But you are initializing it with a shortcut:

f := []&lt;type&gt;{{...}, {...}}

is roughly the same as:

f := []&lt;type&gt;{&lt;type&gt;{...}, &lt;type&gt;{...}}

But it's a little smarter than blind string replacement. If &lt;type&gt; is a pointer like *Ex or *Track, it also automatically initializes correctly:

f := []*&lt;type&gt;{{...}, {...}} // is the same as...
f := []*&lt;type&gt;{&amp;&lt;type&gt;{...}, &amp;&lt;type&gt;{...}}

It works the same for maps:

f := map[string]*&lt;type&gt;{&quot;a&quot;: {...}, &quot;b&quot;: {...}} // is the same as...
f := map[string]*&lt;type&gt;{&quot;a&quot;: &amp;&lt;type&gt;{...}, &quot;b&quot;: &amp;&lt;type&gt;{...}}

For further clarification, anonymous structs are ones that have no separate type definition. These are anonymous types, but not anonymous structs. An anonymous struct is a struct with no associated type definition. This syntax of initializing values without referring to the type is essential to making the manageable:

f := []struct{
    A, B int
}{
    {1, 2}, {3, 4}
}
// or with pointers...
f := []*struct{
    A, B int
}{
    {1, 2}, {3, 4}
}

Here the struct within the list has no type definition, so it is anonymous. And thanks to the initialization shorthand, that's fine - I don't need a name to initialize it.

答案2

得分: 0

请详细说明一下你对Ex结构的定义。

但是,如果Ex被实现为以下形式:

type Ex struct {
    t interface{}
    o interface{}
}

你没有给出匿名结构体的指针,因为定义是未知的。

此外,你不能这样做:

variable := new({1, 2})
// 或者
variable := {1, 2}

当你这样做时:

b := []Ex{{1, 2}, {3, 4}}
// 你创建了一个包含两个Ex结构的切片
// 所以 b := []Ex{Ex{t:1,o:2}, Ex{t:3, o:4}}

相反地:

f := []*Ex{&{1, 2}, &{3, 4}}
// 你创建了一个未知定义类型的切片
// f = []Ex{Ex{t: addressOfUnknowType, o: addressOfUnknowType}}

希望对你有所帮助。

英文:

Please give more detail on your structure definition of Ex.

But, if Ex is implemented as this:

type Ex struct {
    t interface{}
    o interface{}
}

you don't give a pointer of anonymous struct because, the definition it's not know.

Also you can't do:

variable := new({1, 2})
// or
variable := {1, 2}

When you do:

b := []Ex{{1, 2}, {3, 4}}
// you create slice of Ex with two Ex struct
// so b := []Ex{Ex{t:1,o:2}, Ex{t:3, o:4}}

contrariwise:

f := []*Ex{&amp;{1, 2}, &amp;{3, 4}}
// you create slice of unknow definition type
// f = []Ex{Ex{t: addressOfUnknowType, o: addressOfUnknowType}}

I hope help you

huangapple
  • 本文由 发表于 2017年8月12日 00:23:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/45639721.html
匿名

发表评论

匿名网友

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

确定