Help us shape the future of Go! Take the 2026 Go Developer Survey

robots

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package robots renders and serves /robots.txt (RFC 9309): groups of user agents with their allow and disallow rules, plus Sitemap lines. Nothing is served unless the app registers it:

err := robots.Register(r, robots.File{
	Groups: []robots.Group{
		{UserAgents: []string{"*"}, Disallow: []string{"/admin/", "/trap/"}},
	},
	Sitemaps: []string{"https://shop.example/sitemap.xml"},
	NoIndex:  app.Config.NoIndex, // staging: disallow everything
})

Values are single-line: a line break or control character in a user agent or rule is an error, so input cannot add rules.

Index

Constants

View Source
const DisallowAll = "User-agent: *\nDisallow: /\n"

DisallowAll is the body served when File.NoIndex is set.

Variables

This section is empty.

Functions

func Handler

func Handler(f File) (http.Handler, error)

Handler serves f, rendered once, with an ETag and Cache-Control, answering conditional requests with 304.

func Register

func Register(r *router.Router, f File) error

Register serves f at GET /robots.txt.

func Write

func Write(w io.Writer, f File) error

Write writes f to w, after Validate.

Types

type File

type File struct {
	Groups []Group
	// Sitemaps are absolute http(s) URLs written as Sitemap lines.
	Sitemaps []string
	// NoIndex replaces the groups with one that disallows everything for
	// every crawler (a staging site); the sitemaps are left out too.
	NoIndex bool
}

File describes robots.txt.

func (File) Validate

func (f File) Validate() error

Validate reports a group without a user agent, a rule that is not a path and any value that would span lines.

type Group

type Group struct {
	// UserAgents are the product tokens the group applies to ("*",
	// "Googlebot"). At least one is required.
	UserAgents []string
	// Allow and Disallow are path rules: "/" followed by the path, with
	// "*" and a final "$" as RFC 9309 wildcards; "#" (a comment) is
	// refused. An empty Disallow entry
	// writes "Disallow:", which allows everything.
	Allow    []string
	Disallow []string
}

Group is one record of the file: rules for the crawlers it names.

Jump to

Keyboard shortcuts

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