Skip to content

Instantly share code, notes, and snippets.

@techjanitor
Created October 12, 2014 09:16
Show Gist options
  • Save techjanitor/224b221702a77b0faf79 to your computer and use it in GitHub Desktop.
Save techjanitor/224b221702a77b0faf79 to your computer and use it in GitHub Desktop.
Generate Google sitemap
package main
import (
"bufio"
"encoding/xml"
"fmt"
"os"
)
func main() {
MakeSitemap()
}
func MakeSitemap() {
f := bufio.NewWriter(os.Stdout)
sitemap := Sitemap{}
sitemap.Xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9"
sitemap.XmlnsImage = "http://www.google.com/schemas/sitemap-image/1.1"
// Add root Address
root := UrlList{Location: "http://test.com/", ChangeFreq: "hourly"}
image := ImageList{Image: "http://test.com/test.jpg"}
root.Image = append(root.Image, image)
sitemap.Url = append(sitemap.Url, root)
output, err := xml.MarshalIndent(sitemap, " ", " ")
if err != nil {
fmt.Println("error")
}
f.Write([]byte(xml.Header))
f.Write(output)
f.Flush()
}
type Sitemap struct {
XMLName xml.Name `xml:"urlset"`
Xmlns string `xml:"xmlns,attr"`
XmlnsImage string `xml:"xmlns:image,attr"`
Url []UrlList `xml:"url"`
}
type UrlList struct {
Location string `xml:"loc"`
Lastmod string `xml:"lastmod,omitempty"`
ChangeFreq string `xml:"changefreq,omitempty"`
Image []ImageList `xml:"image:image,omitempty"`
}
type ImageList struct {
Image string `xml:"image:loc"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment