Go ไม่มี inheritance — แต่ใช้ composition ผ่าน struct embedding แทน โดยการฝัง (embed) struct หนึ่ง (หรือ interface) ไว้ในอีกตัวหนึ่ง ชนิดที่อยู่ภายนอกจะได้รับ field และ method ของชนิดที่อยู่ภายในมาโดยตรง นี่คือคำตอบของ Go สำหรับการนำโค้ดกลับมาใช้ใหม่ ที่นิยม composition แบบ "has-a" มากกว่า inheritance แบบ "is-a"
การ embed struct
Animal {
Name
}
Eat() {
a.Name +
}
Dog {
Animal
Breed
}
d := Dog{
Animal: Animal{Name: },
Breed: ,
}
fmt.Println(d.Name)
fmt.Println(d.Eat())
