https://github.com/caledoniaproject/gopkg-sitemap
Simple and effective golang sitemap generator package
https://github.com/caledoniaproject/gopkg-sitemap
Last synced: about 1 year ago
JSON representation
Simple and effective golang sitemap generator package
- Host: GitHub
- URL: https://github.com/caledoniaproject/gopkg-sitemap
- Owner: CaledoniaProject
- License: mit
- Created: 2024-12-14T12:27:42.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-20T13:48:54.000Z (over 1 year ago)
- Last Synced: 2025-02-10T00:44:19.045Z (over 1 year ago)
- Language: Go
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
## Introduction
Once again I'm unable to find a suitable sitemap generator, so I created my own package. This one is optimized for memory efficiency and meets most standard requirements.
## Usage example
Module installation
```bash
go get github.com/CaledoniaProject/gopkg-sitemap
```
Module usage
```go
import (
"github.com/CaledoniaProject/gopkg-sitemap"
)
// your source of sitemap
type MyArticle struct {
ThumbnailURL []string
CanonicalURL string
CreatedAt time.Time
}
// major function to create sitemap and index file
func WriteSitemap(articles []*MyArticle) error {
sg := &sitemap.SitemapGenerator{
OutputDirectory: "/tmp/sitemap", // where to save these files
LinksPerFile: 5000, // adjust this parameter to avoid search engine limits
}
for _, article := range articles {
sitemapURL := &sitemap.SitemapURL{
Loc: article.CanonicalURL,
LastMod: article.CreatedAt,
ChangeFreq: sitemap.Daily,
Priority: 0.4,
}
for _, thumbnail := range article.ThumbnailURL {
sitemapURL.Images = append(sitemapURL.Images, &sitemap.SitemapImage{Loc: thumbnail})
}
if err := sg.AddURL(sitemapURL); err != nil {
return err
}
}
// the base url of your sitemap, aka where is it hosted
return sg.WriteIndex("http://cdn.example.com")
}
```
Output directory structure
```bash
/var/www/cdn.example.com/sitemap
- sitemap-index.xml
- file-1.gz
- ...
- file-10.gz
```