tags:

views:

66

answers:

2

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 in Go? If so, what's the correct syntax?

+2  A: 

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.

peterSO
A: 

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.

newacct