Home > Backend Development > Golang > How to provide a value to an imported embedded structure literal?

How to provide a value to an imported embedded structure literal?

WBOY
Release: 2024-02-05 21:54:11
forward
1150 people have browsed it

How to provide a value to an imported embedded structure literal?

Question content

This is a newbie:) I can not understand

When I do this in a file:

scratch.go

package main

import "fmt"

type foo struct {
    field1 string
    field2 string
}

type bar struct {
    foo
    field3 string
    field4 string
}

func main() {
    foobar := bar{
        foo{
            "apples",
            "banana",
        },
        "spam",
        "eggs",
    }
    fmt.printf("%#v\n", foobar)

}
Copy after login

It works But when I have 3 files like this

rootproject
├── magazine
│   ├── address.go
│   └── employee.go
└── main.go
Copy after login

magazine/address.go

package magazine

type address struct {
    street     string
    city       string
    state      string
    postalcode string
}
Copy after login

magazine/employee.go

package magazine

type employee struct {
    name   string
    salary float64
    address
}
Copy after login

and main.go

package main

import (
    "fmt"
    "magazine"
)

func main() {
    employee := magazine.employee{
        name:   "pogi",
        salary: 69420,
        magazine.address{
            street:     "23 pukinginamo st.",
            city:       "bactol city",
            state:      "betlog",
            postalcode: "23432",
        },
    }

    fmt.printf("%#v\n", employee)

}
Copy after login

mistake:(

mixture of field:value and value elements in struct literal
Copy after login

I don't understand, what did I do wrong? I think if the structure is nested, it is said to be embedded in the outer structure and I can access the inner structure's fields from the outer structure. This is the case with my first example (single file), but when I do this in a package. Is there any difference?


Correct answer


I think if the structure is nested, it is said to be embedded in the outer structure and I can access the fields of the inner structure from the outer structure .

Yes, you can directly access members of an embedded field, but this is done using compound literals. If you look at the rules for structured literals, you'll find the following:

If any element has a key, then every element must have a key.

This rule applies regardless of whether the field is embedded or not.

To fix the error, you can delete other keys:

func main() {
    employee := magazine.employee{
        "pogi",
        69420,
        magazine.address{
            street:     "23 pukinginamo st.",
            city:       "bactol city",
            state:      "betlog",
            postalcode: "23432",
        },
    }
    fmt.printf("%#v\n", employee)
}
Copy after login

Or you can specify all keys:

func main() {
    employee := magazine.Employee{
        Name:   "pogi",
        Salary: 69420,
        Address: magazine.Address{
            Street:     "23 pukinginamo st.",
            City:       "bactol city",
            State:      "betlog",
            PostalCode: "23432",
        },
    }
    fmt.Printf("%#v\n", employee)
}
Copy after login

Note that for embedded fields, you can reference the embedded field using the unqualified name of the type.

https://www.php.cn/link/2eeb0ca3f02a275d5179f3b6d9e86e7d

Fields declared with a type but without an explicit field name are called embedded fields. Embedded fields must be specified as type names t or pointers to non-interface type names *t, and t itself may not be a pointer type. Unqualified type names act as field names.

The above is the detailed content of How to provide a value to an imported embedded structure literal?. 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