arrays - 为什么 *S Slice 可以由文字 S 开头?

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

我还没有搜索到它的规范,如果有请给我链接。

最佳答案

来自ref spec

Within a composite literal of array, slice, or map type T, elements or map keys that are themselves composite literals may elide the respective literal type if it is identical to the element or key type of T. Similarly, elements or keys that are addresses of composite literals may elide the &T when the element or key type is *T.

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

关于arrays - 为什么 *S Slice 可以由文字 S 开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39189950/

相关文章:

go - 删除文件的前 N ​​行

json - 下面的json数据在golang中的结构表示是什么?

postgresql - 无法从 db.QueryRow() 推断出错误

python - 带有权重的 numpy 数组部分和

javascript - 根据带有下划线的数组对对象进行排序

c - 如何将未知大小的字符串数组传递给 C 中的函数

arrays - 如何在子 slice 中拆分 Go 中的 slice

python - Python 中的 DICOM 切片厚度

递归打印方法中的数组

java - 数组垂直显示而不是水平显示