Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
type Animal struct{}
type Dog struct{ Animal }
func (a Animal) Kind() string { return "pet" }
func (a Animal) Say() string { return a.Kind() }
func (d Dog) Kind() string { return "dog" }
func main() { fmt.Println(Dog{}.Kind(), Dog{}.Say()) }dog pet
- Expecting
dog dog:Sayis a method ofAnimal, so itsa.Kind()callsAnimal.Kind; embedding is not inheritance, andDog's method is never reached. - Expecting
pet petor a compile error:Dog's ownKindshadows the promoted one, soDog{}.Kind()calls it without any conflict.
- How do you still reach an embedded field after an own field shadows it?
- If two embedded types promote the same method name, what happens?