使用根级元素进行XML解析

Is it possible to parse root level XML elements?

This XML is without any wrapper <message att='Hello'/>

var x = Xml{}
xml.Unmarshal([]byte(`<message att='Hello'/>`), &x)
fmt.Println(x)

Xml Struct

type Xml struct {
    Message struct {
        Att string `xml:"att,attr"`
    } `xml:"message"`
}

Yes, you can do this. Simply remove the wrapping Xml element and unmarshal Message directly:

type Message struct {
    Att string `xml:"att,attr"`
}
var x = Message{}
err := xml.Unmarshal([]byte(`<message att='Hello'/>`), &x)
if err != nil {
    panic(err)
}
fmt.Println(x)

https://play.golang.org/p/EdtaWLm6Cl