golang get a struct from an interface via reflection

Answer #1 100 %

You don't want to pass a pointer to the interface, you want to pass in a pointer to your struct itself.

robot := &Robot{}


The moment you assigned robot to iface, you created a copy of the robot value. There's is no way to ever get a reference back to robot from iface.

When you pass in f(&iface), the call to reflect.ValueOf(i).Elem() is just returning the inner iface value, not a Robot struct value.

You’ll also like:

© 2023 CodeForDev.com -