Last active
August 29, 2015 14:19
-
-
Save yuya-matsushima/209ba57b95d0da84f84e to your computer and use it in GitHub Desktop.
sitemap.xmlを読み込んでデータをよしなにする
This file contains 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" | |
"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