https://github.com/rakarmp/simigen
Automatically create sitemap.xml
https://github.com/rakarmp/simigen
golang learn method sitemap
Last synced: 3 months ago
JSON representation
Automatically create sitemap.xml
- Host: GitHub
- URL: https://github.com/rakarmp/simigen
- Owner: rakarmp
- License: mit
- Created: 2023-09-08T14:56:07.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-29T10:50:51.000Z (11 months ago)
- Last Synced: 2024-07-17T05:25:55.115Z (10 months ago)
- Topics: golang, learn, method, sitemap
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
simigen
![]()
Automatically create sitemap.xml
### Example
```go
package mainimport (
"encoding/xml"
"fmt"
"os"
"time"
)type SitemapIndex struct {
XMLName xml.Name `xml:"sitemapindex"`
Xmlns string `xml:"xmlns,attr"`
Sitemap []Sitemap
}type Sitemap struct {
Loc string `xml:"loc"`
LastMod time.Time `xml:"lastmod"`
}func main() {
urls := []string{
"https://example.com/page1",
"https://example.com/page2",
"https://example.com/page3",
}sitemap := GenerateSitemap(urls)
if err := SaveSitemap(sitemap, "sitemap.xml"); err != nil {
fmt.Println("Failed to save sitemap:", err)
return
}fmt.Println("Sitemap generated successfully!")
}func GenerateSitemap(urls []string) SitemapIndex {
var sitemapIndex SitemapIndex
now := time.Now()for _, url := range urls {
sitemap := Sitemap{
Loc: url,
LastMod: now,
}
sitemapIndex.Sitemap = append(sitemapIndex.Sitemap, sitemap)
}return sitemapIndex
}func SaveSitemap(sitemap SitemapIndex, filename string) error {
sitemapIndexXml, err := xml.MarshalIndent(sitemap, "", " ")
if err != nil {
return err
}xmlStr := xml.Header + string(sitemapIndexXml)
if err := os.WriteFile(filename, []byte(xmlStr), os.ModePerm); err != nil {
return err
}return nil
}```
### With Go Fiber
```go
package mainimport (
"encoding/xml"
"github.com/gofiber/fiber/v2"
"io/ioutil"
"time"
)type SitemapIndex struct {
XMLName xml.Name `xml:"sitemapindex"`
Xmlns string `xml:"xmlns,attr"`
Sitemap []Sitemap
}type Sitemap struct {
Loc string `xml:"loc"`
LastMod time.Time `xml:"lastmod"`
}func main() {
app := fiber.New()app.Get("/sitemap.xml", func(c *fiber.Ctx) error {
urls := []string{
"https://example.com/page1",
"https://example.com/page2",
"https://example.com/page3",
}
sitemap := GenerateSitemap(urls)// Set the response headers
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationXMLCharsetUTF8)
c.Set(fiber.HeaderContentDisposition, "attachment; filename=sitemap.xml")// Marshal the sitemap to XML
sitemapXML, err := xml.MarshalIndent(sitemap, "", " ")
if err != nil {
return err
}// Return the sitemap XML as response
return c.Send(sitemapXML)
})err := app.Listen(":3000")
if err != nil {
panic(err)
}
}func GenerateSitemap(urls []string) SitemapIndex {
var sitemapIndex SitemapIndex
now := time.Now()for _, url := range urls {
sitemap := Sitemap{
Loc: url,
LastMod: now,
}
sitemapIndex.Sitemap = append(sitemapIndex.Sitemap, sitemap)
}return sitemapIndex
}```