Skip to content

Instantly share code, notes, and snippets.

@krasin
Created March 29, 2012 06:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krasin/2234273 to your computer and use it in GitHub Desktop.
Save krasin/2234273 to your computer and use it in GitHub Desktop.
An example of parsing XML in Go
package main
import (
"encoding/xml"
"fmt"
"log"
)
const data = `<Note>
<To>Tove</To>
<From>Jani</From>
<Title>Reminder</Title>
<Body>Don't forget me this weekend!</Body>
</Note>`
type Note struct {
To string
From string
Title string
Body string
}
func main() {
var v Note
if err := xml.Unmarshal([]byte(data), &v); err != nil {
log.Fatal("xml.Unmarshal: ", err)
}
fmt.Printf("note: %v\n", v)
}
@krasin
Copy link
Author

krasin commented Mar 29, 2012

The expected output is:

$ go run hello_xml.go
note: {Tove Jani Reminder Don't forget me this weekend!}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment