import ( "encoding/xml" "fmt" ) type Pkg struct { PkgType int `xml:"P"` //P:包类型 } type Message struct { XMLName xml.Name `xml:"DP"` Pkg Name string `xml:"D"` Age int //`xml:"X"` }
msg := &Message{ Pkg: Pkg{1}, Name: "jack", Age: 9} buf, err := xml.Marshal(msg) fmt.Println(err, string(buf)) msg1 := &Message{} err = xml.Unmarshal(buf, msg1) fmt.Println(err, msg1)
输出:
<nil> <DP><P>1</P><D>jack</D><Age>9</Age></DP>
<nil> &{{ DP} {1} jack 9}