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

WBOY
Release: 2024-02-10 12:42:17
forward
361 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]). Since user contains person, the literal user must contain the literal person, as follows:

    u := user{ 
        person: person{
            name: "bob",
        },
        email: "<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="a5c7cac7e5c7cac7d6d5cad18bc6cac8">[email&#160;protected]</a>",
        password: "you're kidding right?",
    } 
Copy after login

However, once you have a variable of type user, you can leverage the anonymous field to set (or get) the anonymous person# via user ## of name:

    u := user{}
    u.name = "bob"
    u.email = "<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="8fede0edcfede0edfcffe0fba1ece0e2">[email&#160;protected]</a>",
    u.password = "you're kidding right?",
Copy after login

Why does go make me do all this work?

Let's imagine that the inner

person could 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

user structure and add its own name field:

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

Now you

can obviously initialize the new name field:

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

Note that this is the same code that initialized

user.person.name before, but now it is initializing user.name. not good.

More questions

There are more traps hidden in such code.

First, add the

name field in user already similarly "destroy" the user variable on the name Unqualified reference to :

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

Additionally, using only anonymous

person fields, the user.person.name field is marshaled by default to json as the "name" field:

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

If the

name field is added, this is the field marshalled as "name", and user.person.name Fields are not grouped at all >.

You might think you could add a

json tag to user.person.name, like

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

But

now person is marshaled into an object with a name field:

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

This can happen if you try to change the name of a marshaled field for an anonymous person, even though user does not have a name field .

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
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!