I have a simple code:
type Namer interface {
PrintName()
}
type P struct {
Name string
}
func (p *P) PrintName() {
fmt.Printf("%s
", p.Name)
}
func main() {
p := P{Name: "Name"}
var namers []Namer
namers = append(namers, &p)
fmt.Println(reflect.TypeOf(namers[0]))
on := &namers[0]
fmt.Println(reflect.TypeOf(on))
(*on).PrintName()
(**on).Name = "EEEE"
(*on).PrintName()
}
and bunch of questions :)
- Why I cannot write: append(namers, p)? &p is a pointer to P, array namers is not a array of pointers
- Why TypeOf(namers[0]) is *P and TypeOf(on) is *Namer? It does not make sense, TypeOf(&(*P)) should be **P
- Why the last line print: "Name" instead of "EEE"?
Thanks for help!