type person struct{
Name string
Age int
}
// parameters : (pointer to person struct), which is basically address of person object
func printPerson(p *person) {
// when we add '*' to a address, then it becomes dereferencing, Hence
// I read "*p.Name" as "person object dot Name" and i expect it to give value,
// I get this error:
// ./prog.go:20:15: invalid indirect of p.Name (type string)
// ./prog.go:20:24: invalid indirect of p.Age (type int)
fmt.Println(*p.Name, *p.Age) // does not works, ERROR THROWN
// But this works perfectly
// I read it as "person address dot name and person address dot age"
// for me it does not make sense when we say "address dot field name",
// shouldn't it be "object dot field name ? "
fmt.Println(p.Name, p.Age)
}
func main() {
p := person{"foobar", 23}
printPerson(&p) // we are sending address to the method
}
why can't we execute dereferenced object dot field name instead of address dot field name ? Please read code comments for question explanation, what am i missing here ?