如果我有一个接口被传递给一个函数,有没有办法判断传入的项目是一个结构体还是一个指向结构体的指针?我写了这个愚蠢的测试来说明我需要弄清楚什么。
type MyStruct struct {
Value string
}
func TestInterfaceIsOrIsntPointer(t *testing.T) {
var myStruct interface{} = MyStruct{Value: "hello1"}
var myPointerToStruct interface{} = &MyStruct{Value: "hello1"}
// the IsPointer method doesn't exist on interface, but I need something like this
if myStruct.IsPointer() || !myPointerToStruct.IsPointer() {
t.Fatal("expected myStruct to not be pointer and myPointerToStruct to be a pointer")
}
}
月关宝盒
慕斯王
湖上湖
相关分类