sitemap

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2024 License: MIT Imports: 7 Imported by: 20

README

XML sitemap

#Usage

  import (
    "github.com/denisbakhtin/sitemap"
    "time"
    ...
  )

  func CreateSitemap() {
    folder = "public_sitemap_folder"
    domain := "http://mydomain.com"
    now := time.Now()
    pages := make([]sitemap.Page, 1)

    //Home page
    pages = append(pages, sitemap.Page{
      Loc:        fmt.Sprintf("%s", domain),
      LastMod:    now,
      Changefreq: sitemap.Daily,
      Priority:   1,
    })

    //more pages
    published := models.GetPublishedPages() //get slice of pages
    for i := range published {
      pages = append(pages, sitemap.Page{
        Loc:        fmt.Sprintf("%s/pages/%d", domain, published[i].Id), //page url
        LastMod:    published[i].UpdatedAt, //page modification timestamp (time.Time)
        Changefreq: sitemap.Weekly, //or "hourly", "daily", ...
        Priority:   0.8,
      })
    }
    if err := sitemap.SiteMap(path.Join(folder, "sitemap1.xml.gz"), pages); err != nil {
      log.Error(err)
      return
    }
    if err := sitemap.SiteMapIndex(folder, "sitemap_index.xml", domain+"/public/sitemap/"); err != nil {
      log.Error(err)
      return
    }
    //done
  }

For periodical sitemap generation pair it with something like this: https://github.com/jasonlvhit/gocron

Documentation

Overview

Package sitemap provides structures and functions for web-site xml-sitemap management.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SiteMap

func SiteMap(f string, pages []Page) error

Sitemap creates an .xml.gz sitemap file with name f for a list of pages.

func SiteMapIndex

func SiteMapIndex(folder, indexFile, baseurl string) error

SiteMapIndex creates an .xml index file named indexFile for all .xml.gz sitemap files in folder. Baseurl is a base web url for this folder.

Types

type Frequency

type Frequency int

Frequency represents a web-page refresh rate

const (
	Always Frequency = iota
	Hourly
	Daily
	Weekly
	Monthly
	Yearly
	Never
)

Predefined constants for page refresh frequency. The value "always" should be used to describe documents that change each time they are accessed. The value "never" should be used to describe archived URLs. Please note that the value of this tag is considered a hint and not a command.

func (Frequency) String

func (f Frequency) String() string

type Page

type Page struct {
	Loc        string
	LastMod    time.Time
	Changefreq Frequency
	Priority   float32
}

Page represents a web-page record in a sitemap file. Loc is an absolute page URL. LastMod is a timestamp of page's last modification. Changefreq is how often page'c content is updated on server. Use constants Always, Hourly,... Never to set this. Priority is an arbitrary decimal number from 0 to 1 (ex: 0.7) that sets an importance priority for a search engine crawler.

func (*Page) String

func (page *Page) String() string

String returns a string representation of Page using a prefefined template

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL