Created
October 14, 2018 19:19
-
-
Save igorzakhar/56dc848fc977eeea231514eff5765fb9 to your computer and use it in GitHub Desktop.
Unmarshal xml response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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