Skip to content

Instantly share code, notes, and snippets.

@larryprice
Last active February 21, 2018 00:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larryprice/204fec2e8d33979f8cac to your computer and use it in GitHub Desktop.
Save larryprice/204fec2e8d33979f8cac to your computer and use it in GitHub Desktop.
Sample code used for blog post on parsing XML with Go
// Sample code used for blog post on parsing XML with Go
// https://larry-price.com
//
// Go Playground: https://play.golang.org/p/qiSoxxb5tp
package main
import (
"encoding/xml"
"fmt"
)
var personXML = []byte(`
<person>
<name>Luann Van Houten</name>
<addresses>
<address type="secondary">
<street>321 MadeUp Lane</street>
<city>Shelbyville</city>
</address>
<address type="primary">
<street>123 Fake St</street>
<city>Springfield</city>
</address>
</addresses>
</person>`)
type Person struct {
Name string `xml:"name"`
Addresses []struct {
Street string `xml:"street"`
City string `xml:"city"`
Type string `xml:"type,attr"`
} `xml:"addresses>address"`
}
func main() {
var luann Person
xml.Unmarshal(personXML, &luann)
fmt.Println(luann)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment