Skip to content

Instantly share code, notes, and snippets.

@igorzakhar
Created October 14, 2018 19:19
Show Gist options
  • Save igorzakhar/56dc848fc977eeea231514eff5765fb9 to your computer and use it in GitHub Desktop.
Save igorzakhar/56dc848fc977eeea231514eff5765fb9 to your computer and use it in GitHub Desktop.
Unmarshal xml response
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type URLSet struct {
XMLName xml.Name `xml:"urlset"`
URLS []URL `xml:"url"`
}
type URL struct {
XMLName xml.Name `xml:"url"`
Loc string `xml:"loc"`
}
func main() {
resp, err := http.Get("https://www.coursera.org/sitemap~www~courses.xml")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var urls URLSet
xml.Unmarshal(body, &urls)
for i := 0; i < len(urls.URLS); i++ {
fmt.Println(urls.URLS[i].Loc)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment