Can't decode XML with golang, always empty struct

前端 未结 1 1350
清酒与你
清酒与你 2021-01-24 20:53

I\'m trying to decode XML with golang, but the following code gives an empty struct

Anyone can help?

When I run the following code, I always get



        
1条回答
  •  囚心锁ツ
    2021-01-24 21:13

    You need to export your struct fields as per https://golang.org/pkg/encoding/xml/#Unmarshal

    Because Unmarshal uses the reflect package, it can only assign to exported (upper case) fields. Unmarshal uses a case-sensitive comparison to match XML element names to tag values and struct field names.

    e.g.

    type Proto struct {
        XMLName xml.Name `xml:"field"`
        Name      string `xml:"name,attr"`
        Shownameg string `xml:"showname,attr"`
        Fields []Field
    }
    

    0 讨论(0)
提交回复
热议问题