0

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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.