go1.6 embed
时间: 2023-10-22 20:05:21 浏览: 37
The Go programming language allows for the embedding of one type within another type. This is known as embedding or composition.
For example, let's say we have a struct called "Person" that has fields for name, age, and address. We can then create another struct called "Employee" that embeds the "Person" struct and adds additional fields for employee ID and job title.
Here's an example code snippet:
```
type Person struct {
name string
age int
address string
}
type Employee struct {
Person
employeeID string
jobTitle string
}
func main() {
p := Person{name: "John Smith", age: 30, address: "123 Main St"}
e := Employee{Person: p, employeeID: "E1234", jobTitle: "Software Engineer"}
fmt.Println(e.name, e.age, e.address, e.employeeID, e.jobTitle)
}
```
In this example, we create a new Employee struct by embedding the Person struct within it. We can then access the fields of the embedded struct using dot notation (e.g. e.name).
Embedding can be a powerful tool for structuring and organizing code, as it allows for easy reuse of common fields and methods.