Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go-如何定义结构字段';s数据类型作为另一个结构_Go - Fatal编程技术网

Go-如何定义结构字段';s数据类型作为另一个结构

Go-如何定义结构字段';s数据类型作为另一个结构,go,Go,如何将struct的字段类型定义为struct 我希望能够拥有如下内容: type HelloResp struct { Response struct `xml:resp` } func (hr *HelloResp) SetHelloResp(interf interface{}) { hr.Response = interf } 基本上,我有一些其他的子结构,我想根据需要嵌入在HelloResp.Response下,这样它们就可以通过函数互换了 这是可能的,还是有什么推荐的方法

如何将struct的字段类型定义为struct

我希望能够拥有如下内容:

type HelloResp struct {
  Response struct `xml:resp`
}

func (hr *HelloResp) SetHelloResp(interf interface{}) {
  hr.Response = interf
}
基本上,我有一些其他的子结构,我想根据需要嵌入在
HelloResp.Response
下,这样它们就可以通过函数互换了


这是可能的,还是有什么推荐的方法可以做到这一点?

如果使用
innerxml
字段标记,则可以延迟处理,直到知道结构中的内容。为此,您可能需要一个HTTP头或一个提供类型的字段。然后根据该类型解组响应内容

type HelloResp struct {
    ResponseType string `xml:responseType`
    Response []byte `xml:response,innerxml`
}
如果结构具有类型为[]字节或字符串且带有标记“innerxml”的字段,则Unmarshal会累积嵌套在该字段中元素内的原始XML。其余规则仍然适用


另一个(不太可取的)选项是将所有可能包含的类型列为指针。解组器将填充它找到的一个(多个)。您需要弄清楚设置了哪一个,因此无论哪种方式都需要响应类型。

我不想使用解组,因为它使用反射,对性能不好。我必须尝试使用指针,并以某种方式在结构级别动态指向您已经在使用的解组器。第一种方法只是延迟了嵌入式结构的处理。您是否分析了应用程序?解组是瓶颈吗?