结构指针的切片字面量的 golang 快捷语法
golang shortcut syntax for slice literal of pointers to structs
给出
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.
给出
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.