Home >Backend Development >Golang >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!
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 }
But it will produce this structure:
user := user{person: person{name: ""}, email: "", password: ""}
How can I do something like this:
type person struct { name string } type user struct { name person.name // here email, password string }
Use it like this
user := User{Name: "", Email: "", Password: ""}
is it possible?
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 protected]</a>", password: "you're kidding right?", }
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 protected]</a>", u.password = "you're kidding right?",Why does go make me do all this work?
person could be initialized the way you are looking for:
u := user{ name: "bob" }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 }Now you
can obviously initialize the new name field:
u := user{ name: "bob" }Note that this is the same code that initialized
user.person.name before, but now it is initializing
user.name. not good.
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.nameAdditionally, using only anonymous
person fields, the
user.person.name field is marshaled by default to json as the "name" field:
{ "name": "", "email": "", "password": "" }If the
name field is added,
this is the field marshalled as "name", and
user.person.name Fields are not grouped at all
>.
json tag to
user.person.name, like
type user struct { person `json:"personname"` name string email string password string }But
now person is marshaled into an
object with a
name field:
{ "PersonName": { "Name": "" }, "Name": "", "Email": "", "Password": "" }
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
.
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!