Given an interface, how do I obtain a pointer to the underlying value?
My naive attempt was to use a type assertion like this:
var mytypeptr *MyType = myinterface.(*MyType)
But I get:
interface conversion: MyInterface is MyType, not *MyType
Given an interface, how do I obtain a pointer to the underlying value?
My naive attempt was to use a type assertion like this:
var mytypeptr *MyType = myinterface.(*MyType)
But I get:
interface conversion: MyInterface is MyType, not *MyType
You could start with, using reflect.Indirect()
:
val := reflect.ValueOf(myinterface)
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}