sitemap

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2023 License: GPL-3.0 Imports: 5 Imported by: 0

README

Go Reference CI Status

A GO library for generating Sitemap XML files.

Example

package main

import (
	"bytes"
	"fmt"
	"io"
	"time"

	sitemap "github.com/PlanitarInc/go-sitemap"
)

type SitemapOutput struct {
	IndexBuf   bytes.Buffer
	UrlsetBufs []bytes.Buffer
}

func (out *SitemapOutput) Index() io.Writer {
	return &out.IndexBuf
}

func (out *SitemapOutput) Urlset() io.Writer {
	out.UrlsetBufs = append(out.UrlsetBufs, bytes.Buffer{})
	return &out.UrlsetBufs[len(out.UrlsetBufs)-1]
}

type ArrayInput struct {
	Arr     []sitemap.UrlEntry
	nextIdx int
}

func (a *ArrayInput) Next() *sitemap.UrlEntry {
	if a.nextIdx >= len(a.Arr) {
		return nil
	}

	a.nextIdx++
	return &a.Arr[a.nextIdx-1]
}

func (a *ArrayInput) GetUrlsetUrl(n int) string {
	return fmt.Sprintf("https://goiguide.com/sitemap-%d.xml", n)
}

func main() {
	entries := []sitemap.UrlEntry{
		{
			Loc:     "http://goiguide.com/",
			LastMod: time.Date(2025, time.November, 2, 11, 34, 58, 123, time.UTC),
		},
		{
			Loc: "http://goiguide.com/test/",
			Images: []string{
				"http://goiguide.com/test/1.jpg",
				"http://goiguide.com/test/2.jpg",
				"http://goiguide.com/test/3.jpg",
			},
		},
	}

	var out SitemapOutput
	err := sitemap.WriteAll(&out, &ArrayInput{Arr: entries})
	if err != nil {
		fmt.Printf("Error: %s\n", err)
	}

	for i := range out.UrlsetBufs {
		fmt.Printf("\n\n::: Urlset %d\n\n", i)
		fmt.Print(out.UrlsetBufs[i].String())
	}
	fmt.Printf("\n\n::: Index\n\n")
	fmt.Print(out.IndexBuf.String())
}

The output:

::: Urlset 0

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
  <url>
    <loc>http://goiguide.com/</loc>
    <lastmod>2025-11-02T11:34:58Z</lastmod>
  </url>
  <url>
    <loc>http://goiguide.com/test/</loc>
    <image:image>
      <image:loc>http://goiguide.com/test/1.jpg</image:loc>
    </image:image>
    <image:image>
      <image:loc>http://goiguide.com/test/2.jpg</image:loc>
    </image:image>
    <image:image>
      <image:loc>http://goiguide.com/test/3.jpg</image:loc>
    </image:image>
  </url>
</urlset>

::: Index

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>https://goiguide.com/sitemap-0.xml</loc>
  </sitemap>
</sitemapindex>

Benchmark

The library is trying to be smart when producing XML and keeps the allocations constant. That is, space complexity is O(1) -- it does not depend on the number of entries.

$ go test -run '^$' -bench . -benchmem
goos: darwin
goarch: amd64
pkg: github.com/PlanitarInc/go-sitemap
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkWriteAll/1-12                      	 1328336	       945.2 ns/op	     128 B/op	       3 allocs/op
BenchmarkWriteAll/10-12                     	  150031	      6808 ns/op	     128 B/op	       3 allocs/op
BenchmarkWriteAll/100-12                    	   17624	     62340 ns/op	     128 B/op	       3 allocs/op
BenchmarkWriteAll/1000-12                   	    1898	    627547 ns/op	     128 B/op	       3 allocs/op
BenchmarkWriteAll/10000-12                  	     192	   6315018 ns/op	     128 B/op	       3 allocs/op
BenchmarkWriteAll/100000-12                 	      18	  62742897 ns/op	     160 B/op	       4 allocs/op
BenchmarkWriteAll/1000000-12                	       2	 632302752 ns/op	     736 B/op	      22 allocs/op

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteAll added in v1.3.1

func WriteAll(o Output, in Input) error

WriteAll writes all files to the given output. Urlset files are written to writers provided by o.Urlset(), the function will call it every time a new file is to be written. The final index file is written to a writer provided by o.Index(). The function aborts if any unexpected error occurs when writing.

Example (Buffers)
entries := []sitemap.UrlEntry{
	{
		Loc:     "http://goiguide.com/",
		LastMod: time.Date(2025, time.November, 2, 11, 34, 58, 123, time.UTC),
	},
	{
		Loc: "http://goiguide.com/test/",
		Images: []string{
			"http://goiguide.com/test/1.jpg",
			"http://goiguide.com/test/2.jpg",
			"http://goiguide.com/test/3.jpg",
		},
	},
}

var out bufferOutput
err := sitemap.WriteAll(&out, &arrayInput{Arr: entries})
if err != nil {
	fmt.Printf("Error: %s\n", err)
}

for i := range out.UrlsetBufs {
	fmt.Printf("\n\n::: Urlset %d\n\n", i)
	fmt.Print(out.UrlsetBufs[i].String())
}
fmt.Printf("\n\n::: Index\n\n")
fmt.Print(out.IndexBuf.String())
Output:

::: Urlset 0

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
  <url>
    <loc>http://goiguide.com/</loc>
    <lastmod>2025-11-02T11:34:58Z</lastmod>
  </url>
  <url>
    <loc>http://goiguide.com/test/</loc>
    <image:image>
      <image:loc>http://goiguide.com/test/1.jpg</image:loc>
    </image:image>
    <image:image>
      <image:loc>http://goiguide.com/test/2.jpg</image:loc>
    </image:image>
    <image:image>
      <image:loc>http://goiguide.com/test/3.jpg</image:loc>
    </image:image>
  </url>
</urlset>

::: Index

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>https://goiguide.com/sitemap-0.xml</loc>
  </sitemap>
</sitemapindex>
Example (DynamicInput)
err := sitemap.WriteAll(&stdoutOutput{}, &dynamicInput{Length: 3})
if err != nil {
	fmt.Printf("Error: %s\n", err)
}
Output:

Example (Stdout)
entries := []sitemap.UrlEntry{
	{
		Loc:     "http://goiguide.com/",
		LastMod: time.Date(2025, time.November, 2, 11, 34, 58, 123, time.UTC),
	},
	{
		Loc: "http://goiguide.com/test/",
		Images: []string{
			"http://goiguide.com/test/1.jpg",
			"http://goiguide.com/test/2.jpg",
			"http://goiguide.com/test/3.jpg",
		},
	},
}

err := sitemap.WriteAll(&stdoutOutput{}, &arrayInput{Arr: entries})
if err != nil {
	fmt.Printf("Error: %s\n", err)
}
Output:

::: Urlset 0

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
  <url>
    <loc>http://goiguide.com/</loc>
    <lastmod>2025-11-02T11:34:58Z</lastmod>
  </url>
  <url>
    <loc>http://goiguide.com/test/</loc>
    <image:image>
      <image:loc>http://goiguide.com/test/1.jpg</image:loc>
    </image:image>
    <image:image>
      <image:loc>http://goiguide.com/test/2.jpg</image:loc>
    </image:image>
    <image:image>
      <image:loc>http://goiguide.com/test/3.jpg</image:loc>
    </image:image>
  </url>
</urlset>

::: Index

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>https://goiguide.com/sitemap-0.xml</loc>
  </sitemap>
</sitemapindex>

Types

type ChannelInput

type ChannelInput struct {
	// contains filtered or unexported fields
}

func NewChannelInput

func NewChannelInput(getUrlsetUrl func(int) string) *ChannelInput

func (*ChannelInput) Close

func (in *ChannelInput) Close()

func (*ChannelInput) Feed

func (in *ChannelInput) Feed(entry *UrlEntry)

func (*ChannelInput) GetUrlsetUrl added in v1.3.1

func (in *ChannelInput) GetUrlsetUrl(n int) string

func (*ChannelInput) Next

func (in *ChannelInput) Next() *UrlEntry

type Input

type Input interface {
	// Next returns the next UrlEntry to be written. The function should
	// return nil if and only if there are no more items.
	Next() *UrlEntry
	// GetUrlsetUrl returns a URL for the Urlset file at the given index.
	GetUrlsetUrl(idx int) string
}

type Output added in v1.3.1

type Output interface {
	Index() io.Writer
	Urlset() io.Writer
}

type UrlEntry

type UrlEntry struct {
	Loc     string
	LastMod time.Time
	Images  []string
}

Jump to

Keyboard shortcuts

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