package sitemap import ( "net/http" "strconv" "time" ) type ChangeFreq string const ( ChangeFreqAlways ChangeFreq = "always" ChangeFreqNever ChangeFreq = "never" ChangeFreqHourly ChangeFreq = "hourly" ChangeFreqDaily ChangeFreq = "daily" ChangeFreqWeekly ChangeFreq = "weekly" ChangeFreqMonthly ChangeFreq = "monthly" ChangeFreqYearly ChangeFreq = "yearly" ) type URL struct { Loc string ChangeFreq ChangeFreq Priority float64 LastMod time.Time } type internalURL struct { Loc string ChangeFreq ChangeFreq Priority string // Date in ISO 8601 format. LastMod string } func convertURLs(urlset []URL) []internalURL { res := make([]internalURL, 0, len(urlset)) for _, url := range urlset { res = append(res, internalURL{ Loc: url.Loc, ChangeFreq: url.ChangeFreq, Priority: formatPriority(url.Priority), LastMod: formatLastMod(url.LastMod), }) } return res } func formatLastMod(lastMod time.Time) string { if lastMod.IsZero() { return "" } return lastMod.Format("2006-01-02") } func formatPriority(priority float64) string { if priority < 0.0 { priority = 0.0 } if priority > 1.0 { priority = 1.0 } return strconv.FormatFloat(priority, 'f', 1, 64) } func Execute(w http.ResponseWriter, urlset []URL) error { if parsingError != nil { return parsingError } w.Header().Set("Content-Type", "application/xml") internalURLSet := convertURLs(urlset) return parsedTemplate.Execute(w, internalURLSet) }