Skip to content
Sitemap Generator in Golang
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
LICENSE
README.md
go.mod
sitemap.go

README.md

Golang Sitemap Builder

GoDoc

Import

import "github.com/jor-go/sitemap"

Usage

package main

import (
    "github.com/jor-go/sitemap"
    "time"
    "fmt"
)

func main() {
    var mySitemap sitemap.Sitemap
    siteLinks := []string{
        "https://example.com/page-1",
        "https://example.com/page-2",
        "https://example.com/page-3",
    }

    for _, link := range siteLinks {
        url := sitemap.URL{}
        err := url.New(link, "daily", 0.5, time.Now())
        if err != nil {
            fmt.Println(err)
            break
        }
        mySitemap.AddURL(url)
    }

    err := mySitemap.GenerateAndSave("/tmp/sitemap.xml")
    if err != nil {
        fmt.Println(err)
    }
}

Output

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
      <loc>https://example.com/page-1</loc>
      <lastmod>2019-03-19</lastmod>
      <changefreq>daily</changefreq>
      <priority>0.5</priority>
   </url>
   <url>
      <loc>https://example.com/page-2</loc>
      <lastmod>2019-03-19</lastmod>
      <changefreq>daily</changefreq>
      <priority>0.5</priority>
   </url>
   <url>
      <loc>https://example.com/page-3</loc>
      <lastmod>2019-03-19</lastmod>
      <changefreq>daily</changefreq>
      <priority>0.5</priority>
   </url>
</urlset>

Generate Sitemap as Bytes

bytes, err := mySitemap.Generate()
if err != nil {
    fmt.Println(err)
}

// use bytes
You can’t perform that action at this time.