Struct Embedding
package main
import "fmt"
type Person struct {
Name string
Age int
}
func (p Person) SayHello() {
fmt.Printf("Hello, my name is %s and I'm %d years old\n", p.Name, p.Age)
}
type Employee struct {
Person
Company string
}
func main() {
emp := Employee{
Person: Person{
Name: "John",
Age: 30,
},
Company: "ABC Inc.",
}
emp.SayHello()
}Last updated