type Address struct {
Street string
City string
Country string
}
type Person struct {
Name string
Age int
Address Address // ← Вложенная структура
} func main() {
p := Person{
Name: "Alice",
Age: 30,
Address: Address{
Street: "123 Main St",
City: "New York",
Country: "USA",
},
}
fmt.Println(p.Name) // ← Alice
fmt.Println(p.Address.City) // ← New York
fmt.Println(p.Address.Country) // ← USA
} // Способ 1: инициализация всех полей сразу
p1 := Person{
Name: "Bob",
Age: 25,
Address: Address{
Street: "456 Oak Ave",
City: "Boston",
Country: "USA",
},
}
// Способ 2: пошаговая инициализация
p2 := Person{
Name: "Charlie",
Age: 35,
}
p2.Address.Street = "789 Pine Rd"
p2.Address.City = "Seattle"
p2.Address.Country = "USA"
// Способ 3: создание адреса отдельно
addr := Address{
Street: "321 Elm St",
City: "Chicago",
Country: "USA",
}
p3 := Person{
Name: "Diana",
Age: 28,
Address: addr,
} type Address struct {
Street string
City string
Country string
}
type Person struct {
Address // ← Встроенная структура (без имени поля)
Name string
Age int
} func main() {
p := Person{
Name: "Alice",
Age: 30,
}
// Можно обращаться к полям Address напрямую
p.Street = "123 Main St"
p.City = "New York"
p.Country = "USA"
fmt.Println(p.City) // ← New York
// Но также можно обращаться через имя типа
fmt.Println(p.Address.City) // ← New York
} type Address struct {
Street string
City string
Country string
}
func (a Address) FullAddress() string {
return fmt.Sprintf("%s, %s, %s", a.Street, a.City, a.Country)
}
type Person struct {
Address
Name string
Age int
}
func main() {
p := Person{
Name: "Alice",
Age: 30,
Address: Address{
Street: "123 Main St",
City: "New York",
Country: "USA",
},
}
// Метод Address доступен напрямую через Person
fmt.Println(p.FullAddress()) // ← 123 Main St, New York, USA
} type Employee struct {
ID int
Name string
}
func (e Employee) GetInfo() string {
return fmt.Sprintf("Employee #%d: %s", e.ID, e.Name)
}
type Manager struct {
Employee // ← Manager "является" Employee с дополнительными свойствами
Team []string
}
func (m Manager) GetTeamSize() int {
return len(m.Team)
} type Customer struct {
ID int
Name string
Email string
}
type Order struct {
ID int
Customer Customer // ← Order "имеет" Customer
Items []string
Total float64
} type Printable struct{}
func (p Printable) Print() {
fmt.Println("Printing...")
}
type Saveable struct{}
func (s Saveable) Save() {
fmt.Println("Saving...")
}
type Document struct {
Printable // ← Первое встраивание
Saveable // ← Второе встраивание
Title string
Content string
}
func main() {
doc := Document{Title: "Report", Content: "..."}
doc.Print() // ← Метод от Printable
doc.Save() // ← Метод от Saveable
} type A struct {
Value int
}
func (a A) Display() {
fmt.Println("Display from A, Value:", a.Value)
}
type B struct {
Value int
}
func (b B) Display() {
fmt.Println("Display from B, Value:", b.Value)
}
type C struct {
A
B
}
func main() {
c := C{}
// Конфликт полей
// c.Value = 10 // ← Ошибка компиляции: ambiguous selector c.Value
c.A.Value = 10 // ← Нужно явно указать, какой Value
c.B.Value = 20
// Конфликт методов
// c.Display() // ← Ошибка компиляции: ambiguous selector c.Display
c.A.Display() // ← Display from A, Value: 10
c.B.Display() // ← Display from B, Value: 20
} type Celsius float64
type Fahrenheit float64
func (c Celsius) ToFahrenheit() Fahrenheit {
return Fahrenheit(c*9/5 + 32)
}
func (f Fahrenheit) ToCelsius() Celsius {
return Celsius((f - 32) * 5 / 9)
}
func main() {
temp := Celsius(20.0)
fmt.Printf("%.1f°C = %.1f°F\n", temp, temp.ToFahrenheit()) // ← 20.0°C = 68.0°F
}