[英]Golang anonymous struct in initializing slice of pointers
From Chapter 7 of GOPL (Section 7.6), I noticed this line: 从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")},
}
I was kind of confused by how it initialized the slice of Track
pointers. 我对它如何初始化Track
指针片段感到困惑。 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. 我发现所有5个案例都没事,没有错误。 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. {1, 2}
似乎是一个名为“匿名结构”的结构,但它让我感到困惑,因为它工作正常,即使我试图填写*Ex
指针而不是Ex
结构。
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? 有人可以帮助解释这些情况下实际发生的情况吗?
I think anonymous isn't quite the correct word here. 我认为匿名在这里不是正确的词。 The struct has a name, "Track" or "Ex". 该结构具有名称“Track”或“Ex”。 But you are initializing it with a shortcut: 但是您正在使用快捷方式初始化它:
f := []<type>{{...}, {...}}
is roughly the same as: 大致相同:
f := []<type>{<type>{...}, <type>{...}}
But it's a little smarter than blind string replacement. 但它比盲目替换更聪明一些。 If <type>
is a pointer like *Ex
or *Track
, it also automatically initializes correctly: 如果<type>
是*Ex
或*Track
类的指针,它也会自动正确初始化:
f := []*<type>{{...}, {...}} // is the same as...
f := []*<type>{&<type>{...}, &<type>{...}}
It works the same for maps: 它对地图的作用相同:
f := map[string]*<type>{"a": {...}, "b": {...}} // is the same as...
f := map[string]*<type>{"a": &<type>{...}, "b": &<type>{...}}
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. 并且由于初始化速记,这很好 - 我不需要名称来初始化它。
Please give more detail on your structure definition of Ex. 请详细说明您的Ex的结构定义。
But, if Ex is implemented as this: 但是,如果Ex实现如下:
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{&{1, 2}, &{3, 4}}
// you create slice of unknow definition type
// f = []Ex{Ex{t: addressOfUnknowType, o: addressOfUnknowType}}
I hope help you 我希望能帮助你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.