How to start a go structure with a nested structure whose name has the package name

PHPz
Release: 2024-02-06 09:45:16
forward
817 people have browsed it

如何使用名称具有包名称的嵌套结构来启动 go 结构

Question content

I have a go structure defined as follows:

type record struct {
    events.apigatewayproxyrequest          `json:",omitempty"`
    events.apigatewaywebsocketproxyrequest `json:",omitempty"` //nolint:all
    events.sqsevent                        `json:",omitempty"`
}
Copy after login

I want to know how to start this structure. I have tried before:

Record{events.APIGatewayProxyRequest: {}}
Copy after login

But it gives me an error: invalid field name events.apigatewayproxyrequest in struct literal . It seems that names containing package names cannot be used as key names in structures. What's the correct way to start it?


Correct answer


When embedding a type into a structure, the field names of the enclosing structure are the same as the type names of the embedded type (without the package selector). so:

event:=Record{
  APIGatewayProxyRequest: events.APIGatewayProxyRequest{ ... },
}
Copy after login

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

The above is the detailed content of How to start a go structure with a nested structure whose name has the package name. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!