给定
type foo struct {
id int
}
两者看起来是一样的
var foos = []*foo{
{1},
{2},
{3}}
var foos = []*foo{
&foo{1},
&foo{2},
&foo{3}}
这是为什么呢?虽然在 TGPL 中找不到它的提及。
最佳答案
可以跳过composite literals中的类型.
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.
关于pointers - 结构指针的 slice 字面量的 golang 快捷语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48767698/