Taking address and returning pointer to a local variable inside a function in Go causes compiler to allocate it on heap rather than stack, so that returned pointer remains valid.
Now what happens if I take and return address of a struct member or embedded struct ?
type A struct {
a,b,c int
}
type B struct {
A
d,e,f int
}
func (b *B) get1() *A {
return &b.A
}
func (b *B) get2() *A {
localB := B{}
return &localB.A
}
Will compiler allocate embedded struct A on heap and keep members of B on stack ?
Will garbage collector collect localB even if localB.A reference is still in use ?
How can compiler determine when to keep embedded struct on stack or heap in case if it's accessed through reflection ?