所以我可以拥有
struct {
int
x []int
}
但是,
struct {
int
[]int
}
将导致语法错误:unexpected [, expecting }
。有没有办法在 Go 的结构中使用未命名的数组?如果是这样,正确的语法是什么?
最佳答案
阅读The Go Programming Language Specification .特别是关于 Struct types 的部分.描述您正在寻找的内容的 Go 术语是一个匿名字段。
Such a[n] [anonymous] field type must be specified as a type name T or as a pointer to a type name *T, and T itself may not be a pointer type.
int
是 type name . []int
既不是类型名称也不是指向类型名称的指针。
关于syntax - Go 结构中的未命名数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3129324/