go - How can I tell if my interface{} is a pointer? -
if have interface being passed function, there way tell if item passed in struct or pointer struct? wrote silly test illustrate need figure out.
type mystruct struct { value string } func testinterfaceisorisntpointer(t *testing.t) { var mystruct interface{} = mystruct{value: "hello1"} var mypointertostruct interface{} = &mystruct{value: "hello1"} // ispointer method doesn't exist on interface, need if mystruct.ispointer() || !mypointertostruct.ispointer() { t.fatal("expected mystruct not pointer , mypointertostruct pointer") } }
func isstruct(i interface{}) { return reflect.valueof(i).type().kind == reflect.struct } you can test via changing type according needs such reflect.ptr. can pointed value reflect.indirect(reflect.valueof(i)) after ensured it's pointer.
addition:
it seems reflect.value has kind method reflect.valueof(i).kind() enough.
Comments
Post a Comment