为什么* S Slice可以由文字S初始化?

时间:2016-08-28 09:43:49

标签: arrays go slice

我在the go programming language的第187页看到了这种语法。

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

它只是

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

我没有使用Google的说明书,如果有,请给我链接。

1 个答案:

答案 0 :(得分:2)

来自ref spec

  

在数组,切片或地图类型T的复合文字中,如果它们与T的元素或键类型相同,那么它们本身是复合文字的元素或映射键可能会忽略相应的文字类型。类似地,元素或键当元素或键类型为* T时,复合文字的地址可能会忽略& T.

[...]*Point{{1.5, -3.5}, {0, 0}}    // same as [...]*Point{&Point{1.5, -3.5}, &Point{0, 0}}