Unnamed arrays in structs in Go
So I can have
struct {
int
x []int
}
However,
struct {
int
[]int
}
will result in a syntax error: unexpected [, expecting }
. Is there a way of having unnamed arrays in structs i开发者_如何学运维n Go? If so, what's the correct syntax?
Read The Go Programming Language Specification. In particular, the section on Struct types. The Go term to describe what you are looking for is an anonymous field.
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
is a type name. []int
is neither a type name nor a pointer to a type name.
No, the type of an anonymous field must be a type name or a pointer to a type name. You could declare a new type name that is the same as an array type, and then it would work, but it wouldn't be exactly the same.
精彩评论