2

I have a go struct defined as below:

type Record struct {
    events.APIGatewayProxyRequest          `json:",omitempty"`
    events.APIGatewayWebsocketProxyRequest `json:",omitempty"` //nolint:all
    events.SQSEvent                        `json:",omitempty"`
}

I wonder how I can initiate this struct. I tried:

Record{events.APIGatewayProxyRequest: {}}

but it gives me an error: invalid field name events.APIGatewayProxyRequest in struct literal. It seems the name including package name can't be used as key name in the struct. What is the right way to initiate it?

1 Answer 1

1

When you embed a type into a struct, the enclosing struct has a field name that is the same as the type name of the embedded type without the package selector. So:

event:=Record{
  APIGatewayProxyRequest: events.APIGatewayProxyRequest{ ... },
}

The right hand side of the initialization is a literal of that type, so you use the full type name (with the selector).

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

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.