type Whisperer interface { Whisper() // <- changes to whisperer will propagate to Talker}type Yeller interface { Yell()}// embedding these interfaces into Talkertype Talker interface { Whisperer Yeller}func main() { t := // some talker t.Whisper() t.Yell()}
Embedding structs
type Account struct { id, balance int}func (a Account) GetId() int { return a.id}func (a Account) String() string { return fmt.Sprintf("Regular account")}// ManagerAccount has an embedded structtype ManagerAccount struct { Account}func (m ManagerAccount) String() string { return fmt.Sprintf("Manager account")}func main() { ma := ManagerAccount{Account{1, 2000}} // since the account is embedded the following will work managerId := ma.GetId() managerBalance := ma.balance fmt.Println(ma.String()) // Will print "Manager account" fmt.Println(ma.Account.String()) // Will print "Regular account"}