This is a creation in Article, where the information may have evolved or changed.
XML is widely used as a format for data exchange and information transmission. With the increasing application of Web services, XML is now playing an increasingly important role in the daily development work. In this section, we will describe the packages of XML related processing in the Go Language standard pack. This section does not cover XML specification-related content (refer to other literature for more information), but rather describes how to encode XML file-related knowledge in the Go language. If you are an OPS person, you generate the following XML configuration file for all the servers you administer: <?xml version= "1.0" encoding= "Utf-8"?>
shanghai_vpn
127.0.0.1
beijing_vpn
127.0.0.2
Gotest Project Main.gopackage mainimport ("Encoding/xml" "FMT" "Io/ioutil" "OS") type recurlyservers struct {xmlname xm L.name ' xml: "Servers" ' Version string ' xml: "version,attr" ' Svs []server ' xml: ' Server ' ' Description string ' XM L: ", InnerXml" '}type server struct {xmlname XML. Name ' xml: ' Server ' ' ServerName string ' xml: ' ServerName ' ' ServerIP string ' xml: ' ServerIP ' '}func main () {file, err: = OS . Open ("Servers.xml") if err! = Nil {fmt. Println ("Error: &v", err) return}data, err: = Ioutil. ReadAll (file) if err! = Nil {fmt. Println ("Error &v", err) return}v: = Recurlyservers{}err = XML. Unmarshal (data, &v) if err! = Nil {fmt. Println ("Error &v", err) return}fmt. Println (V.xmlname) fmt. Println (v.version) fmt. Println ("--------------------") fmt. Println (V.svs[0]. ServerName) fmt. Println (V.svs[0]. ServerIP) fmt. Println ("--------------------") fmt. Println (V.svs[1]. ServerName) fmt. Println (V.svs[1]. ServerIP)}