package sitemaps import ( "encoding/xml" "fmt" "strings" "time" ) const ( xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9" ) type ChangeFreq string func (c *ChangeFreq) UnmarshalText(text []byte) error { v, ok := map[string]ChangeFreq{ "always": Always, "hourly": Hourly, "daily": Daily, "weekly": Weekly, "monthly": Monthly, "yearly": Yearly, "never": Never, }[strings.ToLower(string(text))] if !ok { return fmt.Errorf("Unknown change frequency %s", text) } else { *c = v } return nil } func (c ChangeFreq) MarshalText() ([]byte, error) { return []byte(c), nil } const ( Always ChangeFreq = "always" Hourly = "hourly" Daily = "daily" Weekly = "weekly" Monthly = "monthly" Yearly = "yearly" Never = "never" ) type SitemapTime time.Time func (t *SitemapTime) UnmarshalText(text []byte) error { p, err := time.Parse(time.RFC3339, string(text)) if err != nil { return err } *t = SitemapTime(p) return nil } func (t SitemapTime) MarshalText() ([]byte, error) { return []byte(time.Time(t).Format(time.RFC3339)), nil } type Sitemap struct { XMLName xml.Name Urls []*SiteUrl `xml:"url"` } type SiteUrl struct { Location string `xml:"loc"` LastModified *time.Time `xml:"lastmod,omitempty"` ChangeFrequency ChangeFreq `xml:"changefreq,omitempty"` Priority float32 `xml:"priority,omitempty"` } func NewSitemap() *Sitemap { return &Sitemap{ XMLName: xml.Name{Local: "urlset", Space: xmlns}, } }