Documentation
¶
Overview ¶
Package sitemap provides tools for creating XML sitemaps and sitemap indexes and writing them to io.Writer (such as http.ResponseWriter).
Please see http://www.sitemaps.org/ for description of sitemap contents and https://developers.google.com/search/docs/advanced/sitemaps/news-sitemap for the news sitemap documentation.
Package sitemap provides tools for creating XML sitemaps and sitemap indexes and writing them to io.Writer (such as http.ResponseWriter).
Please see http://www.sitemaps.org/ for description of sitemap contents.
Example ¶
package main
import (
"os"
"time"
"github.com/semantosoph/sitemap"
)
func main() {
sm := sitemap.New()
t := time.Unix(0, 0).UTC()
sm.Add(&sitemap.URL{
Loc: "http://example.com/",
LastMod: sitemap.Date{t},
ChangeFreq: sitemap.Daily,
})
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>1970-01-01</lastmod> <changefreq>daily</changefreq> </url> </urlset>
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 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 Date ¶
Date specifies a proxy time object that we use to define our own XML encoder.
func (Date) MarshalXML ¶
MarshalXML marshals LastMod into the correct format
type News ¶ added in v1.2.0
type News struct {
Pub Publication `xml:"n:publication"`
Date time.Time `xml:"n:publication_date"`
Title string `xml:"n:title"`
Access string `xml:"n:access,omitempty"`
Genres string `xml:"n:genres,omitempty"`
Keywords string `xml:"n:keywords,omitempty"`
StockTickers string `xml:"n:stock_tickers,omitempty"`
}
News is the XML Schema for the News Sitemap extension. Values for publication, date and title are required.
type NewsSitemap ¶ added in v1.2.0
type NewsSitemap struct {
XMLName xml.Name `xml:"urlset"`
NewsXmlns string `xml:"xmlns:n,attr"`
Xmlns string `xml:"xmlns,attr"`
URLs []*NewsURL `xml:"url"`
Minify bool `xml:"-"`
}
NewsSitemap represents a complete news sitemap which can be marshaled to XML. New instances must be created with NewNewsSitemap() in order to set the name space attributes correctly. Minify can be set to make the output smaller, but less human readable.
Example ¶
package main
import (
"os"
"time"
"github.com/semantosoph/sitemap"
)
func main() {
sm := sitemap.NewNewsSitemap()
t := time.Unix(0, 0).UTC()
pub := sitemap.Publication{
Name: "The Testly Chronicle",
Lang: "en",
}
news := sitemap.News{
Pub: pub,
Date: t,
Title: "Hello World",
}
sm.Add(&sitemap.NewsURL{
Loc: "http://example.com/",
News: news,
})
sm.WriteTo(os.Stdout)
}
Output: <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns:n="http://www.google.com/schemas/sitemap-news/0.9" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://example.com/</loc> <n:news> <n:publication> <n:name>The Testly Chronicle</n:name> <n:language>en</n:language> </n:publication> <n:publication_date>1970-01-01T00:00:00Z</n:publication_date> <n:title>Hello World</n:title> </n:news> </url> </urlset>
func (*NewsSitemap) Add ¶ added in v1.2.0
func (s *NewsSitemap) Add(u *NewsURL)
Add adds an URL to a Sitemap.
type Publication ¶ added in v1.2.0
Publication is the publication in which the news appear. Both values are required.
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.
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 ¶
package main
import (
"os"
"time"
"github.com/semantosoph/sitemap"
)
func main() {
smi := sitemap.NewSitemapIndex()
t := time.Unix(0, 0).UTC()
smi.Add(&sitemap.URL{
Loc: "http://example.com/sitemap-1.xml",
LastMod: sitemap.Date{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-01</lastmod> </sitemap> </sitemapindex>
func NewSitemapIndex ¶
func NewSitemapIndex() *SitemapIndex
NewSitemapIndex returns new SitemapIndex.
type URL ¶
type URL struct {
Loc string `xml:"loc"`
LastMod Date `xml:"lastmod,omitempty"`
ChangeFreq ChangeFreq `xml:"changefreq,omitempty"`
Priority float32 `xml:"priority,omitempty"`
}
URL entry in sitemap or sitemap index. 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.