Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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/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
Go:XML将嵌套结构解组到接口{}_Xml_Go_Struct_Interface - Fatal编程技术网

Go:XML将嵌套结构解组到接口{}

Go:XML将嵌套结构解组到接口{},xml,go,struct,interface,Xml,Go,Struct,Interface,我有Python的背景,这是我第一次正式进入Go,所以我认为事情还没有完全解决 我目前正在Go中实现附属窗口XMLAPI。API遵循请求和响应的标准结构,因此为此,我会尽量保持干燥。信封始终具有相同的结构,如下所示: type HeaderStruct struct { Quota string } type Envelope struct { // other fields omitted Header HeaderStruct } headerStruct, ok

我有Python的背景,这是我第一次正式进入Go,所以我认为事情还没有完全解决

我目前正在Go中实现附属窗口XMLAPI。API遵循请求和响应的标准结构,因此为此,我会尽量保持干燥。信封始终具有相同的结构,如下所示:

type HeaderStruct struct {
    Quota string
}

type Envelope struct {
    // other fields omitted
    Header HeaderStruct
}
headerStruct, ok := responseEnvelope.Header.(HeaderStruct)
// if ok is true, headerStruct is of type HeaderStruct
// else responseEnvelope.Header is not of type HeaderStruct

内容
标题
正文
将根据我的请求和响应而有所不同,因此我创建了一个基本
信封
结构

类型信封结构{
XMLName xml.Name`xml:“http://schemas.xmlsoap.org/soap/envelope/ 信封“`
NS1字符串`xml:“xmlns:NS1,attr”`
XSD字符串`xml:“xmlns:XSD,attr”`
标头接口{}`xml:“http://schemas.xmlsoap.org/soap/envelope/ 标题“`
Body接口{}`xml:“Body”`
}
这对于封送请求的XML非常有效,但我在反封送方面遇到了问题:

func新响应开发(主体界面{})*信封{
信封:=新信封()
信封头=&响应头{}
Body=Body
回信信封
}
func main(){
responseBody:=&GetMerchantListResponseBody{}
responseEnvelope:=新建responseEnvelope(responseBody)
b:=字节。NewBufferString(响应)
NewDecoder(b).解码(ResponseDevelope)
fmt.Println(responsedevelope.Header.Quota)//为什么我不能访问这个?
}
这可能在代码中比在文字中更好地描述了这个问题:p

谢谢


Chris

信封结构中的
标题
字段的类型是
接口{}
,它是结构,因此您不能引用它的任何字段

为了引用名为
Quota
的字段,您必须使用包含
Quota
字段的静态类型声明
Header
,如下所示:

type HeaderStruct struct {
    Quota string
}

type Envelope struct {
    // other fields omitted
    Header HeaderStruct
}
headerStruct, ok := responseEnvelope.Header.(HeaderStruct)
// if ok is true, headerStruct is of type HeaderStruct
// else responseEnvelope.Header is not of type HeaderStruct
如果您不知道它将是什么类型,或者无法提交到单个类型,则可以将其保留为
接口{}
,但是您必须在运行时使用或将其转换为静态类型,后者将如下所示:

type HeaderStruct struct {
    Quota string
}

type Envelope struct {
    // other fields omitted
    Header HeaderStruct
}
headerStruct, ok := responseEnvelope.Header.(HeaderStruct)
// if ok is true, headerStruct is of type HeaderStruct
// else responseEnvelope.Header is not of type HeaderStruct
另一种选择是使用反射来访问
Envelope.Header
值的命名字段,但如果可能的话,尝试用其他方法解决它。若你们有兴趣了解更多关于围棋反思的知识,我建议你们先阅读博客文章