gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:09:37 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com