In the below code:
package main
import "fmt"
type People interface {
sayHello()
getDetails()
}
type Person struct {
name string
age int
city, phone string
}
// A person method
func (p Person) sayhello() {
fmt.Printf("Hi, I am %s, form %s
", p.name, p.city)
}
// A person method
func (p Person) getDetails() {
fmt.Printf("[Name: %s, Age: %d, City: %s, Phone: %s]
", p.name, p.age, p.city, p.phone)
}
Person
is structurally typed with People
, because it implemented behavior of People
For the below code:
type Speaker struct {
Person // type embedding for composition
speaksOn []string
pastEvents []string
}
func main() {
obj1 := Speaker{Person{"aa", 23, "ww", "234"}, []string{"aa", "bb"}, []string{"dd", "ee"}}
obj2 := Speaker{Person{"aa", 23, "ww", "234"}, []string{"aa", "bb"}, []string{"dd", "ee"}}
obj3 := Speaker{Person{"aa", 23, "ww", "234"}, []string{"aa", "bb"}, []string{"dd", "ee"}}
peopleList := [...]People{obj1, obj2, obj3}
obj1.getDetails()
obj1.sayhello()
}
Why obj1.getDetails()
is getting resolved? Is it because, Speaker
type has embedded Person
type and Person
type is structurally typed with People
interface type?