-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.go
58 lines (48 loc) · 953 Bytes
/
sitemap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package sitemap
import (
"encoding/xml"
"io"
)
type URL struct {
Loc string `xml:"loc,omitempty"`
Lastmod string `xml:"lastmod,omitempty"`
}
type Sitemap struct {
XMLName xml.Name `xml:"urlset"`
Ns string `xml:"xmlns,attr"`
Writer io.Writer `xml:"-"`
URLs []URL `xml:"url"`
Indent bool `xml:"-"`
}
func NewSitemap(writer io.Writer, indent bool) *Sitemap {
return &Sitemap{
Writer: writer,
URLs: make([]URL, 0),
Ns: "http://www.sitemaps.org/schemas/sitemap/0.9",
Indent: indent,
}
}
func (s *Sitemap) Add(url string, lastmod string) {
s.URLs = append(
s.URLs,
URL{
Loc: url,
Lastmod: lastmod,
},
)
}
func (s *Sitemap) Write() error {
xmlEncoder := xml.NewEncoder(s.Writer)
if s.Indent {
xmlEncoder.Indent("", " ")
}
_, err := s.Writer.Write([]byte(xml.Header))
if err != nil {
return err
}
err = xmlEncoder.Encode(s)
if err != nil {
return err
}
return nil
}