sitemap

package module
v0.0.0-...-9800e5c Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2025 License: MIT Imports: 15 Imported by: 0

README

sitemap

Go Reference Go Report Card

go get git.gorbe.io/go/sitemap@latest

Documentation

Index

Examples

Constants

View Source
const XMLNameSpace = "http://www.sitemaps.org/schemas/sitemap/0.9"

Variables

View Source
var (
	ErrSitemapIndex = errors.New("sitemap is index")
)

Functions

func IsIndex

func IsIndex(data []byte) bool

Types

type ChangeFrequency

type ChangeFrequency []byte

How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page.

Valid values are:

  • always
  • hourly
  • daily
  • weekly
  • monthly
  • yearly
  • never

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. Even though search engine crawlers may consider this information when making decisions, they may crawl pages marked "hourly" less frequently than that, and they may crawl pages marked "yearly" more frequently than that. Crawlers may periodically crawl pages marked "never" so that they can handle unexpected changes to those pages.

Example:

<changefreq>monthly</changefreq>
var (
	ChangeFrequencyAlways  ChangeFrequency = []byte("always")
	ChangeFrequencyHourly  ChangeFrequency = []byte("hourly")
	ChangeFrequencyDaily   ChangeFrequency = []byte("daily")
	ChangeFrequencyWeekly  ChangeFrequency = []byte("weekly")
	ChangeFrequencyMonthly ChangeFrequency = []byte("monthly")
	ChangeFrequencyYearly  ChangeFrequency = []byte("yearly")
	ChangeFrequencyNever   ChangeFrequency = []byte("never")
)

Valid values for ChangeFrequency

func (ChangeFrequency) IsValid

func (c ChangeFrequency) IsValid() bool

ParseChangeFrequency parses ChangeFrequency from v.

If v is not a valid value for ChangeFrequency, returns nil.

func (ChangeFrequency) String

func (c ChangeFrequency) String() string

type Index

type Index struct {
	XMLName   xml.Name `xml:"sitemapindex"`
	NameSpace string   `xml:"xmlns,attr"`
	Sitemap   []URL    `xml:"sitemap"`
}

Index encapsulates information about all of the Sitemaps in the file.

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>http://www.example.com/sitemap1.xml.gz</loc>
    <lastmod>2004-10-01T18:23:17+00:00</lastmod>
  </sitemap>
  <sitemap>
    <loc>http://www.example.com/sitemap2.xml.gz</loc>
    <lastmod>2005-01-01</lastmod>
  </sitemap>
</sitemapindex>

func NewIndex

func NewIndex(sitemaps ...URL) *Index

NewINdex returns a new Index with the given Sitemap Entries.

This function sets the XMLName to "sitemapindex".

type LastModification

type LastModification []byte

The date of last modification of the page. This date should be in W3C Datetime format. This format allows you to omit the time portion, if desired, and use YYYY-MM-DD.

Note that the date must be set to the date the linked page was last modified, not when the sitemap is generated.

Note also that this tag is separate from the If-Modified-Since (304) header the server can return, and search engines may use the information from both sources differently.

Example:

<lastmod>2005-01-01</lastmod>

func (LastModification) String

func (l LastModification) String() string

func (LastModification) Time

func (l LastModification) Time() (time.Time, error)

Time parses the value of LastModification and returns time.Time .

type Location

type Location []byte

Location is the URL of the page. This URL must begin with the protocol (such as http) and end with a trailing slash, if your web server requires it. This value must be less than 2,048 characters.

Example:

<loc>http://www.example.com/</loc>

func (Location) Equal

func (l Location) Equal(loc Location) bool

func (Location) String

func (l Location) String() string

func (Location) URL

func (l Location) URL() (*url.URL, error)

type Priority

type Priority []byte

The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0. This value does not affect how your pages are compared to pages on other sites—it only lets the search engines know which pages you deem most important for the crawlers.

The default priority of a page is 0.5.

Please note that the priority you assign to a page is not likely to influence the position of your URLs in a search engine's result pages. Search engines may use this information when selecting between URLs on the same site, so you can use this tag to increase the likelihood that your most important pages are present in a search index.

Also, please note that assigning a high priority to all of the URLs on your site is not likely to help you. Since the priority is relative, it is only used to select between URLs on your site.

Example:

<priority>0.8</priority>

func (Priority) Float64

func (p Priority) Float64() (float64, error)

func (Priority) String

func (p Priority) String() string

type Sitemap

type Sitemap struct {
	URL []URL
	// contains filtered or unexported fields
}

func CrawlHostname

func CrawlHostname(hostname string, current *Sitemap) (*Sitemap, error)

func Fetch

func Fetch(url string) (*Sitemap, error)

Fetch fetches the Sitemap from url.

If fetch fails, returns the status code as an error (eg.: "404").

func New

func New() *Sitemap

func ParseXML

func ParseXML(data []byte) (*Sitemap, error)

Parse reads the Sitemap from data.

func ParseXMLIndex

func ParseXMLIndex(data []byte) (*Sitemap, error)

func ParseXMLURLSet

func ParseXMLURLSet(data []byte) (*Sitemap, error)

func ReadXML

func ReadXML(r io.Reader) (*Sitemap, error)

Read reads the Sitemap from r.

func ReadXMLFile

func ReadXMLFile(name string) (*Sitemap, error)

ReadFile reads the Sitemap from file the named file.

func (*Sitemap) GetURL

func (s *Sitemap) GetURL(loc string) *URL

func (*Sitemap) IsIndex

func (s *Sitemap) IsIndex() bool

func (*Sitemap) RemoveURL

func (s *Sitemap) RemoveURL(loc string)

func (*Sitemap) SetURL

func (s *Sitemap) SetURL(u *URL)

func (*Sitemap) Size

func (s *Sitemap) Size() int

func (*Sitemap) SortByLocation

func (s *Sitemap) SortByLocation()

SortByLocation sorts the URLs by Location in ascending order.

func (*Sitemap) String

func (s *Sitemap) String() string

func (*Sitemap) ToTXT

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

func (*Sitemap) ToXML

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

func (*Sitemap) ToXMLIndent

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

type URL

type URL struct {
	Location   Location         `xml:"loc"`
	LastMod    LastModification `xml:"lastmod,omitempty"`
	ChangeFreq ChangeFrequency  `xml:"changefreq,omitempty" `
	Priority   Priority         `xml:"priority,omitempty" `
}

URL is the parent tag for each URL entry. The remaining tags are children of this tag.

<url>
  <loc>http://www.example.com/</loc>
  <lastmod>2005-01-01</lastmod>
  <changefreq>monthly</changefreq>
  <priority>0.8</priority>
</url>

func NewURL

func NewURL(loc string) *URL

NewURL returns a new URL with the given fields set. If any field is nil, it will be omitted.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/sitemap"
)

func main() {

	u := sitemap.NewURL("https://example.com").
		SetLastmodification("1970-01-01").
		SetChangeFrequency(sitemap.ChangeFrequencyNever.String()).
		SetPriority("1.0")

	fmt.Printf("%s\n", u)
}
Output:

https://example.com 1970-01-01 never 1.0

func (*URL) SetChangeFrequency

func (u *URL) SetChangeFrequency(changefreq string) *URL

SetChangeFrequency clones changefreq to u.ChangeFreq and returns u.

If URL u is nil, returns nil.

func (*URL) SetLastmodification

func (u *URL) SetLastmodification(lastmod string) *URL

SetLastmodification clones lastmod to u.LastMod and returns u.

If URL u is nil, returns nil.

func (*URL) SetPriority

func (u *URL) SetPriority(priority string) *URL

SetPriority clones priority to u.Priority and returns u.

If URL u is nil, returns nil.

func (URL) String

func (u URL) String() string

type URLSet

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

URLSet encapsulates the file and references the current protocol standard.

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.example.com/</loc>
    <lastmod>2005-01-01</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
</urlset>

func NewURLSet

func NewURLSet(urls ...URL) *URLSet

NewURLSet returns a new URLSet with the given URLs.

Jump to

Keyboard shortcuts

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