Documentation ¶
Overview ¶
Package sitemap provides tools for creating XML sitemaps and sitemap indexes and writing them to io.Writer (such as net/http.ResponseWriter).
Please see https://www.sitemaps.org/ for description of sitemap contents.
Example ¶
This is a web server that implements two request paths /foo and /bar and provides a sitemap that contains those paths at /sitemap.xml.
package main import ( "fmt" "log" "net/http" "github.com/snabb/sitemap" ) func main() { sm := sitemap.New() http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "foo") }) sm.Add(&sitemap.URL{Loc: "http://localhost:8080/foo"}) http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "bar") }) sm.Add(&sitemap.URL{Loc: "http://localhost:8080/bar"}) http.HandleFunc("/sitemap.xml", func(w http.ResponseWriter, r *http.Request) { sm.WriteTo(w) }) log.Fatal(http.ListenAndServe(":8080", nil)) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChangeFreq ¶
type ChangeFreq string
ChangeFreq specifies change frequency of a Sitemap or SitemapIndex URL entry. It is just a string.
const ( Always ChangeFreq = "always" Hourly ChangeFreq = "hourly" Daily ChangeFreq = "daily" Weekly ChangeFreq = "weekly" Monthly ChangeFreq = "monthly" Yearly ChangeFreq = "yearly" Never ChangeFreq = "never" )
Feel free to use these constants for ChangeFreq (or you can just supply a string directly).
type Sitemap ¶
type Sitemap struct { XMLName xml.Name `xml:"urlset"` Xmlns string `xml:"xmlns,attr"` URLs []*URL `xml:"url"` Minify bool `xml:"-"` }
Sitemap represents a complete sitemap which can be marshaled to XML. New instances must be created with New in order to set the xmlns attribute correctly. Minify can be set to make the output less human readable.
Example ¶
Sitemap with one URL.
package main import ( "os" "time" "github.com/snabb/sitemap" ) func main() { sm := sitemap.New() t := time.Date(1984, 1, 1, 0, 0, 0, 0, time.UTC) sm.Add(&sitemap.URL{ Loc: "http://example.com/", LastMod: &t, ChangeFreq: sitemap.Daily, Priority: 0.5, }) sm.WriteTo(os.Stdout) }
Output: <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://example.com/</loc> <lastmod>1984-01-01T00:00:00Z</lastmod> <changefreq>daily</changefreq> <priority>0.5</priority> </url> </urlset>
Example (Minify) ¶
Setting Minify to true omits indentation and newlines in generated sitemap.
package main import ( "os" "time" "github.com/snabb/sitemap" ) func main() { sm := sitemap.New() sm.Minify = true t := time.Date(1984, 1, 1, 0, 0, 0, 0, time.UTC) sm.Add(&sitemap.URL{ Loc: "http://example.com/", LastMod: &t, ChangeFreq: sitemap.Weekly, Priority: 0.5, }) sm.WriteTo(os.Stdout) }
Output: <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>http://example.com/</loc><lastmod>1984-01-01T00:00:00Z</lastmod><changefreq>weekly</changefreq><priority>0.5</priority></url></urlset>
func (*Sitemap) ReadFrom ¶
ReadFrom reads and parses an XML encoded sitemap from io.Reader. Implements io.ReaderFrom.
type SitemapIndex ¶
type SitemapIndex struct { XMLName xml.Name `xml:"sitemapindex"` Xmlns string `xml:"xmlns,attr"` URLs []*URL `xml:"sitemap"` Minify bool `xml:"-"` }
SitemapIndex is like Sitemap except the elements are named differently (and ChangeFreq and Priority may not be used). New instances must be created with NewSitemapIndex in order to set the xmlns attribute correctly. Minify can be set to make the output less human readable.
Example ¶
Sitemap index with one sitemap URL.
package main import ( "os" "time" "github.com/snabb/sitemap" ) func main() { smi := sitemap.NewSitemapIndex() t := time.Unix(0, 0).UTC() smi.Add(&sitemap.URL{ Loc: "http://example.com/sitemap-1.xml", LastMod: &t, }) smi.WriteTo(os.Stdout) }
Output: <?xml version="1.0" encoding="UTF-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>http://example.com/sitemap-1.xml</loc> <lastmod>1970-01-01T00:00:00Z</lastmod> </sitemap> </sitemapindex>
func NewSitemapIndex ¶
func NewSitemapIndex() *SitemapIndex
NewSitemapIndex returns new SitemapIndex.
func (*SitemapIndex) ReadFrom ¶
func (s *SitemapIndex) ReadFrom(r io.Reader) (n int64, err error)
ReadFrom reads and parses an XML encoded sitemap index from io.Reader. Implements io.ReaderFrom.
func (*SitemapIndex) WriteTo ¶
func (s *SitemapIndex) WriteTo(w io.Writer) (n int64, err error)
WriteTo writes XML encoded sitemap index to given io.Writer. Implements io.WriterTo.
type URL ¶
type URL struct { Loc string `xml:"loc"` LastMod *time.Time `xml:"lastmod,omitempty"` ChangeFreq ChangeFreq `xml:"changefreq,omitempty"` Priority float32 `xml:"priority,omitempty"` }
URL entry in Sitemap or SitemapIndex. LastMod is a pointer to time.Time because omitempty does not work otherwise. Loc is the only mandatory item. ChangeFreq and Priority must be left empty when using with a sitemap index.