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?