sitemap

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 14, 2019 License: MIT Imports: 6 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sitemap

type Sitemap struct {
	XMLName xml.Name `xml:"urlset"`
	XMLNS   string   `xml:"xmlns,attr"`
	URLS    []URL    `xml:"url"`
}

Sitemap Holds all the urls in the sitemap

func (*Sitemap) AddURL

func (s *Sitemap) AddURL(u URL)

AddURL Adds a single URL to the sitemap

func (*Sitemap) AddURLs

func (s *Sitemap) AddURLs(u []URL)

AddURLs Adds multiple URLs to the Sitemap

func (*Sitemap) Generate

func (s *Sitemap) Generate() ([]byte, error)

Generate Creates sitemap []byte

func (*Sitemap) GenerateAndSave

func (s *Sitemap) GenerateAndSave(path string) error

GenerateAndSave Generates and saves to filepath e.g. "/path/to/your/sitemap.xml"

type URL

type URL struct {
	Loc        string `xml:"loc"`
	LastMod    string `xml:"lastmod"`
	ChangeFreq string `xml:"changefreq"`
	Priority   string `xml:"priority"`
}

URL is a url for the sitemap

func (*URL) New

func (u *URL) New(location, changeFrequency string, priority float64, lastModified time.Time) error

New Creates a new URL for the sitemap.

location: URL e.g. https://...

changeFrequency: "always", "hourly", "daily", "weekly", "monthly", "yearly", or "never"

priority: float between 0.0 and 1.0

lastModified: time.Time of the last time link was modified

func (*URL) TimeToLastMod

func (u *URL) TimeToLastMod(t time.Time)

TimeToLastMod Convert a time.Time to the last modified date as a string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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