Skip to content

Instantly share code, notes, and snippets.

@yuya-matsushima
Last active August 29, 2015 14:19
Show Gist options
  • Save yuya-matsushima/209ba57b95d0da84f84e to your computer and use it in GitHub Desktop.
Save yuya-matsushima/209ba57b95d0da84f84e to your computer and use it in GitHub Desktop.
sitemap.xmlを読み込んでデータをよしなにする
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"os"
)
type Page struct {
Loc string `xml:"loc"`
LastMod string `xml:"lastmod"`
}
type Sitemap struct {
XMLName xml.Name `xml:"urlset"`
Pages []Page `xml:"url"`
}
func main() {
url := "http://www.e2esound.com/sitemap.xml"
sitemap := GetSitemap(url)
for _, page := range sitemap {
fmt.Println(page.Loc, page.LastMod)
}
}
func GetSitemap(url string) []Page {
response, err := http.Get(url)
checkErr(err)
XMLdata, err := ioutil.ReadAll(response.Body)
checkErr(err)
var sitemap Sitemap
xml.Unmarshal(XMLdata, &sitemap)
return sitemap.Pages
}
func checkErr(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment