Ответ
В Go агрегацию можно реализовать с помощью структур и методов. Например, агрегируем User и Profile:
type Profile struct {
Bio string
Age int
}
type User struct {
Name string
Profile // Агрегация
}
func main() {
user := User{
Name: "Alice",
Profile: Profile{
Bio: "Developer",
Age: 30,
},
}
fmt.Println(user.Name) // Alice
fmt.Println(user.Bio) // Developer (прямой доступ)
fmt.Println(user.Profile) // {Developer 30}
}
Ключевые моменты:
Profileвстраивается вUserбез указания имени поля- Поля
Profileдоступны напрямую черезUser - Можно обращаться и через полный путь
user.Profile.Bio