Skip to content

Instantly share code, notes, and snippets.

Created Nov 5, 2019
What would you like to do?
// :collection Essential Go
package main
import (
// :show start
var xmlStr = `
<person age="34">
<city>San Francisco</city>
<person age="23">
type People struct {
Person []Person `xml:"person"`
type Person struct {
Age int `xml:"age,attr"`
FirstName string `xml:"first-name"`
Address Address `xml:"address"`
type Address struct {
City *string `xml:"city"`
State string `xml:"state"`
// :show end
func main() {
// :show start
var people People
data := []byte(xmlStr)
err := xml.Unmarshal(data, &people)
if err != nil {
log.Fatalf("xml.Unmarshal failed with '%s'\n", err)
fmt.Printf("%#v\n\n", people)
// :show end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment