How to use a struct field in another struct without referencing it as a key

WBOY
Release: 2024-02-10 12:42:17
forward
332 people have browsed it

How to use a struct field in another struct without referencing it as a key

In the development of PHP, we often encounter the situation of using the fields of another structure in one structure. However, referencing it directly as a key can lead to cluttered and unmaintainable code. So how to use structure fields in another structure? PHP editor Baicao provides you with a concise and clear solution to make your code clearer and easier to read. Let’s take a look below!

Question content

I want to insert a structure field into another structure without having to use the structure name.

I know I can do this:

type person struct { name string } type user struct { person email, password string }
Copy after login

But it will produce this structure:

user := user{person: person{name: ""}, email: "", password: ""}
Copy after login

How can I do something like this:

type person struct { name string } type user struct { name person.name // here email, password string }
Copy after login

Use it like this

user := User{Name: "", Email: "", Password: ""}
Copy after login

is it possible?

Solution

Simply put, it cannot be done using the current language implementation.

When initializing a literal, you need to be explicit (or, in other words:literal![sic]). Sinceusercontainsperson, the literalusermust contain the literalperson, as follows:

u := user{ person: person{ name: "bob", }, email: "[email protected]", password: "you're kidding right?", }
Copy after login

However, once you have avariableof typeuser, you can leverage the anonymous field to set (or get) the anonymousperson# viauser## ofname:

u := user{} u.name = "bob" u.email = "[email protected]", u.password = "you're kidding right?",
Copy after login

Why does go make me do all this work?

Let's imagine that the inner

personcould be initialized the way you are looking for:

u := user{ name: "bob" }
Copy after login
Copy after login

Now let's imagine further that we later modify the

userstructure andaddits ownnamefield:

type user struct { person name string email string password string }
Copy after login

Now you

canobviously initialize the newnamefield:

u := user{ name: "bob" }
Copy after login
Copy after login

Note that this is the same code that initialized

user.person.namebefore, but now it is initializinguser.name. not good.

More questions

There are more traps hidden in such code.

First, add the

namefield inuseralreadysimilarly "destroy" theuservariable on thename Unqualified reference to:

u.name = "bob" // used to set user.person.name, now sets user.name
Copy after login

Additionally, using only anonymous

personfields, theuser.person.namefield is marshaled by default to json as the "name" field:

{ "name": "", "email": "", "password": "" }
Copy after login

If the

namefield is added,thisis the field marshalled as"name", anduser.person.nameFields are not grouped at all>.

You might think you could add a

jsontag touser.person.name, like

type user struct { person `json:"personname"` name string email string password string }
Copy after login

But

nowpersonis marshaled into anobjectwith anamefield:

{ "PersonName": { "Name": "" }, "Name": "", "Email": "", "Password": "" }
Copy after login

This can happen if you try to change the name of a marshaled field for an anonymousperson, even thoughuserdoes not have anamefield.

In short: using anonymous structs as a way of "adding fields" within a struct can be problematic and fragile, and should be avoided.

The above is the detailed content of How to use a struct field in another struct without referencing it as a key. 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
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!