How to access fields and methods of an embedded struct in Go?

In Go, you can access fields and methods of an embedded struct by referring to them directly on the outer struct. Here's an example:

package main import "fmt" // Embedding a struct type Person struct { name string age int } // Defining a method on the embedded struct func (p Person) GetName() string { return p.name } // Outer struct type Employee struct { Person // Embedded struct id int } func main() { // Creating an instance of the outer struct emp := Employee{ Person: Person{ name: "John Doe", age: 30, }, id: 123, } // Accessing fields of the embedded struct fmt.Println(emp.name) // Output: John Doe fmt.Println(emp.age) // Output: 30 // Accessing methods of the embedded struct fmt.Println(emp.GetName()) // Output: John Doe }

In the example above, we have an Employee struct that embeds the Person struct. By initializing an instance of Employee and accessing its fields and methods, we can also access the fields and methods of the embedded Person struct directly using dot notation.