Skip to content

Instantly share code, notes, and snippets.

@ewalk153
Created December 25, 2013 17:56
Show Gist options
  • Select an option

  • Save ewalk153/8125351 to your computer and use it in GitHub Desktop.

Select an option

Save ewalk153/8125351 to your computer and use it in GitHub Desktop.
go example for using XML
package main
import (
"encoding/xml"
"fmt"
"log"
)
type Reading struct {
RType string `xml:"r_type,attr"`
ReadingString string `xml:",innerxml"`
}
type Box struct {
Reading Reading `xml:"Reading"`
Raw string `xml:",innerxml"`
}
func main() {
var box Box
Xml := []byte(`<box><Reading r_type="we"><stuff>cool</stuff></Reading></box>`)
if err := xml.Unmarshal(Xml, &box); err != nil {
log.Fatalln("Failed", err)
}
fmt.Printf("Box (xml): %s\n", string(Xml))
fmt.Printf("Box: %#v\n", box)
data, _ := xml.Marshal(Box{Reading: Reading{RType: "we", ReadingString: "<dude>wow</dude>"}, Raw: "<hello>cat</hello>"})
fmt.Printf("Box (xml): %s\n", string(data))
data, _ = xml.Marshal(box)
fmt.Printf("Box (xml): %s\n", string(data))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment