So.....
Forgetting the questions of why I want to do this, I'm hoping someone will have some insight on how to so something.
Say I have the following:
=========================
type Root struct {
Name string
Leaf *Leaf1
}
type Leaf1 struct {
Leaf *Leaf2
}
type Leaf2 struct {}
=========================
I want to be able to use refletion to build this, given only an empty Root{} variable:
top := Root{
Name: "",
Leaf: &Leaf1{
Leaf: &Leaf2{},
},
}
=========================
Walking Root and figuring out what's there is easy, but I'm having trouble with assigning Root.Leaf = &Leaf1{}.
I was experimenting with:
top := Root{}
var p unsafe.Pointer
ptr := reflect.NewAt(reflect.TypeOf(top.Leaf).Elem(), p)
reflect.ValueOf(unsafe.Pointer(top.Leaf)).SetPointer(unsafe.Pointer(ptr.Pointer())) // Wasn't sure if I could use "p" here, but it has the same error.
panic: reflect: reflect.Value.SetPointer using unaddressable value
I've played with a few (dozen) variations, but what I can't seem to get is:
How do I use reflection to get a reflect.Value object that represents the value a pointer points to without knowing it before hand(aka how can I determine Root.Leaf points to Leaf1{}, and create a "Leaf{}" that I can then assign Root.Leaf to points to (wheww).
Anyone got a way?