Pointers 如何在golang中获取使用reflect.New()创建的对象的指针

Pointers 如何在golang中获取使用reflect.New()创建的对象的指针,pointers,go,Pointers,Go,我有一个必须接受对象类型的函数。然后它必须将XML解组到给定类型的对象 使用反射获取对象类型: objType := reflect.TypeOf(CustomObjectType{}) UnmarshalWorker(objType) 解组功能: func UnmarshalFeedGeneric (data []byte,objType reflect.Type) { unm := reflect.New(objType) err := xml.Unmarshal(data

我有一个必须接受对象类型的函数。然后它必须将XML解组到给定类型的对象

使用反射获取对象类型:

objType := reflect.TypeOf(CustomObjectType{})
UnmarshalWorker(objType)
解组功能:

func UnmarshalFeedGeneric (data []byte,objType reflect.Type) {
    unm := reflect.New(objType)
    err := xml.Unmarshal(data, unm.Interface())
}
“unm”总是返回nil值

fmt.Println(unm)
fmt.Println(unm.Elem())
fmt.Println(unm.Interface())
输出:

<*main.CustomObjectType Value>
<main.CustomObjectType Value>
&{ <nil>   0}

&{    0}

它应该可以工作。首先确保您的xml解组正确,否则它将自动失败,因为没有解组的有效xml没有错误,谢谢!我仔细检查了一个代码,在另一个地方发现了一个错误。现在一切都好了。