Documentation
¶
Index ¶
- type AlternateLink
- type ConfigError
- type Image
- type LastModTime
- type NetworkError
- type News
- type NewsPublication
- type ParseError
- type S
- func (s *S) GetErrors() []error
- func (s *S) GetErrorsCount() int64
- func (s *S) GetFetchTimeout() uint16
- func (s *S) GetFollow() []string
- func (s *S) GetHTTPClient() *http.Client
- func (s *S) GetMaxConcurrency() int
- func (s *S) GetMaxDepth() int
- func (s *S) GetMaxResponseSize() int64
- func (s *S) GetMultiThread() bool
- func (s *S) GetRandomURLs(n int) []URL
- func (s *S) GetRules() []string
- func (s *S) GetStrict() bool
- func (s *S) GetURLCount() int64
- func (s *S) GetURLs() []URL
- func (s *S) GetUserAgent() string
- func (s *S) Parse(url string, urlContent *string) (*S, error)
- func (s *S) ParseContext(ctx context.Context, url string, urlContent *string) (*S, error)
- func (s *S) SetFetchTimeout(fetchTimeout uint16) *S
- func (s *S) SetFollow(regexes []string) *S
- func (s *S) SetHTTPClient(client *http.Client) *S
- func (s *S) SetMaxConcurrency(maxConcurrency int) *S
- func (s *S) SetMaxDepth(maxDepth int) *S
- func (s *S) SetMaxResponseSize(maxResponseSize int64) *S
- func (s *S) SetMultiThread(multiThread bool) *S
- func (s *S) SetRules(regexes []string) *S
- func (s *S) SetStrict(strict bool) *S
- func (s *S) SetUserAgent(userAgent string) *S
- type URL
- type URLChangeFreq
- type ValidationError
- type Video
- type VideoPlatform
- type VideoRestriction
- type VideoUploader
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AlternateLink ¶ added in v1.0.0
type AlternateLink struct {
Rel string `xml:"rel,attr"`
Hreflang string `xml:"hreflang,attr"`
Href string `xml:"href,attr"`
}
AlternateLink represents an alternate version of a page (hreflang) per the XHTML standard used in sitemaps. Reference: https://developers.google.com/search/docs/specialty/international/localized-versions#sitemap
type ConfigError ¶ added in v0.9.0
type ConfigError struct {
// Field is the configuration field name (e.g. "maxDepth", "follow", "rules").
Field string
// Err is the underlying validation error.
Err error
}
ConfigError is returned when a configuration setter receives an invalid value. Callers can inspect the Field to determine which configuration setting caused the error.
Example usage:
var cfgErr *sitemap.ConfigError
if errors.As(err, &cfgErr) {
fmt.Println("bad config field:", cfgErr.Field)
}
func (*ConfigError) Error ¶ added in v0.9.0
func (e *ConfigError) Error() string
func (*ConfigError) Unwrap ¶ added in v0.9.0
func (e *ConfigError) Unwrap() error
Unwrap returns the underlying error, enabling errors.Is / errors.As traversal.
type Image ¶ added in v0.7.0
type Image struct {
Loc string `xml:"http://www.google.com/schemas/sitemap-image/1.1 loc"`
Title string `xml:"http://www.google.com/schemas/sitemap-image/1.1 title"`
Caption string `xml:"http://www.google.com/schemas/sitemap-image/1.1 caption"`
GeoLocation string `xml:"http://www.google.com/schemas/sitemap-image/1.1 geo_location"`
License string `xml:"http://www.google.com/schemas/sitemap-image/1.1 license"`
}
Image is a structure of <image:image> in <url>, per the Google Image Sitemap extension. Reference: https://developers.google.com/search/docs/crawling-indexing/sitemaps/image-sitemaps
type LastModTime ¶ added in v1.0.0
func (*LastModTime) UnmarshalXML ¶ added in v1.0.0
func (l *LastModTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
type NetworkError ¶ added in v0.9.0
type NetworkError struct {
// URL is the URL that was being fetched when the error occurred.
URL string
// Err is the underlying network or HTTP error.
Err error
}
NetworkError is returned when an HTTP fetch fails. Callers can inspect URL to determine which resource could not be retrieved.
Example usage:
var netErr *sitemap.NetworkError
if errors.As(err, &netErr) {
fmt.Println("failed to fetch:", netErr.URL)
}
func (*NetworkError) Error ¶ added in v0.9.0
func (e *NetworkError) Error() string
func (*NetworkError) Unwrap ¶ added in v0.9.0
func (e *NetworkError) Unwrap() error
Unwrap returns the underlying error, enabling errors.Is / errors.As traversal.
type News ¶ added in v0.7.0
type News struct {
Publication NewsPublication `xml:"http://www.google.com/schemas/sitemap-news/0.9 publication"`
PublicationDate *LastModTime `xml:"http://www.google.com/schemas/sitemap-news/0.9 publication_date"`
Title string `xml:"http://www.google.com/schemas/sitemap-news/0.9 title"`
}
News is a structure of <news:news> in <url>, per the Google News Sitemap extension. Reference: https://developers.google.com/search/docs/crawling-indexing/sitemaps/news-sitemap
type NewsPublication ¶ added in v0.7.0
type NewsPublication struct {
Name string `xml:"http://www.google.com/schemas/sitemap-news/0.9 name"`
Language string `xml:"http://www.google.com/schemas/sitemap-news/0.9 language"`
}
NewsPublication is a structure of <news:publication> in <news:news>.
type ParseError ¶ added in v0.9.0
type ParseError struct {
// URL is the sitemap URL that was being parsed when the error occurred.
// May be empty when the error is not tied to a specific URL (e.g. max depth reached).
URL string
// Err is the underlying parse error.
Err error
}
ParseError is returned when XML or gzip parsing of a sitemap document fails. Callers can inspect URL to determine which sitemap could not be parsed.
Example usage:
var parseErr *sitemap.ParseError
if errors.As(err, &parseErr) {
fmt.Println("failed to parse sitemap:", parseErr.URL)
}
func (*ParseError) Error ¶ added in v0.9.0
func (e *ParseError) Error() string
func (*ParseError) Unwrap ¶ added in v0.9.0
func (e *ParseError) Unwrap() error
Unwrap returns the underlying error, enabling errors.Is / errors.As traversal.
type S ¶
type S struct {
// contains filtered or unexported fields
}
S is a structure that holds various data related to processing URLs. It contains a cfg field of type `config`, which stores configuration settings. The mainURL field of type string represents the main URL being processed. The mainURLContent field of type string stores the content of the main URL. The robotsTxtSitemapURLs field is a slice of strings that contains the URLs present in the robots.txt file's sitemap directive. The sitemapLocations field is a slice of strings that represents the locations of the sitemap files. The urls field is a slice of URL structs that stores the URLs to be processed. The errs field is a slice of errors that holds any encountered errors during processing.
func New ¶
func New() *S
New creates a new instance of the S structure. It initializes the structure with default configuration values and returns a pointer to the created instance.
func (*S) GetErrorsCount ¶
func (*S) GetFetchTimeout ¶ added in v1.0.0
GetFetchTimeout returns the current fetch timeout in seconds.
func (*S) GetFollow ¶ added in v1.0.0
GetFollow returns a copy of the current follow regex pattern strings.
func (*S) GetHTTPClient ¶ added in v1.0.0
GetHTTPClient returns the custom HTTP client, or nil if the default client behaviour is used.
func (*S) GetMaxConcurrency ¶ added in v1.0.0
GetMaxConcurrency returns the maximum number of concurrent fetch goroutines. A value of 0 means unlimited concurrency.
func (*S) GetMaxDepth ¶ added in v1.0.0
GetMaxDepth returns the maximum recursion depth for following sitemap indexes.
func (*S) GetMaxResponseSize ¶ added in v1.0.0
GetMaxResponseSize returns the maximum allowed HTTP response size in bytes.
func (*S) GetMultiThread ¶ added in v1.0.0
GetMultiThread returns whether multi-threaded fetching and parsing is enabled.
func (*S) GetRandomURLs ¶
GetRandomURLs returns a slice of randomly selected URLs from the S object's URL list. The number of URLs to select is specified by the parameter n. If the S object is nil, an empty slice is returned. The function creates a copy of the original URLs list and randomly selects n URLs from it, removing them to avoid duplicates. The selected URLs are returned as a new slice.
func (*S) GetRules ¶ added in v1.0.0
GetRules returns a copy of the current URL filter regex pattern strings.
func (*S) GetStrict ¶ added in v1.0.0
GetStrict returns whether strict URL validation mode is enabled.
func (*S) GetURLCount ¶
GetURLCount returns the count of URLs in the S struct.
func (*S) GetUserAgent ¶ added in v1.0.0
GetUserAgent returns the current user agent string used for HTTP requests.
func (*S) Parse ¶
Parse is a method of the S structure. It parses the given URL and its content.
Parse is a backward-compatible wrapper around ParseContext that uses context.Background(). For new code, prefer ParseContext so that callers can propagate cancellation and deadlines.
If the S object has any errors, it returns an error with the message "errors occurred before parsing, see GetErrors() for details". It sets the mainURL field to the given URL and the mainURLContent field to the given URL content. It returns an error if there was an error setting the content. If the URL ends with "/robots.txt", it parses the robots.txt file and fetches URLs from the sitemap files mentioned in the robots.txt. If the URL does not end with "/robots.txt", the mainURLContent is checked and unzipped if necessary, then parsed and fetched. It returns the S structure and nil error if the method was able to complete successfully.
func (*S) ParseContext ¶ added in v0.4.0
func (*S) SetFetchTimeout ¶
SetFetchTimeout sets the fetch timeout for the Sitemap Parser. The fetch timeout determines how long the parser will wait for an HTTP request to complete. It should be specified in seconds as a uint16 value and must be greater than 0. Invalid values are ignored and a *ConfigError is recorded. Note: when a custom HTTP client is set via SetHTTPClient, this value has no effect. The function returns a pointer to the S structure to allow method chaining.
func (*S) SetFollow ¶ added in v0.1.3
SetFollow sets the follow patterns using the provided list of regex strings and compiles them into regex objects. Patterns longer than maxRegexPatternLength characters are rejected with a *ConfigError. Any errors encountered during compilation are recorded as *ConfigError values. The function returns a pointer to the S structure to allow method chaining.
func (*S) SetHTTPClient ¶ added in v0.6.0
SetHTTPClient sets a custom HTTP client for the Sitemap Parser. When set, the provided client is used for all HTTP requests instead of the internally created default client. This allows callers to configure custom transports, proxies, TLS settings, authentication, or timeout strategies. When a custom client is provided, SetFetchTimeout has no effect; the client's own Timeout field controls the request deadline. Pass nil to reset to the default client behaviour. The function returns a pointer to the S structure to allow method chaining.
func (*S) SetMaxConcurrency ¶ added in v0.4.0
SetMaxConcurrency sets the maximum number of concurrent fetch goroutines used when multi-threaded parsing is enabled. The default is 16. A value of 0 means unlimited concurrency. A positive value caps the number of in-flight HTTP fetches across the recursive sitemap-index traversal, which is recommended for very large sitemap indexes to avoid goroutine and connection blow-up. Negative values are rejected and a *ConfigError is recorded. The function returns a pointer to the S structure to allow method chaining.
func (*S) SetMaxDepth ¶ added in v0.3.0
SetMaxDepth sets the maximum recursion depth for following sitemap indexes. A sitemap index may reference other sitemap indexes; this limits how many levels deep the parser will follow. The default is 10. The value must be greater than 0; invalid values are ignored and a *ConfigError is recorded. The function returns a pointer to the S structure to allow method chaining.
func (*S) SetMaxResponseSize ¶ added in v0.3.0
SetMaxResponseSize sets the maximum allowed HTTP response size in bytes. Responses exceeding this limit will be truncated and may cause parsing errors. The default is 50 MB, matching the sitemaps.org protocol limit. The value must be greater than 0; invalid values are ignored and a *ConfigError is recorded. The function returns a pointer to the S structure to allow method chaining.
func (*S) SetMultiThread ¶ added in v0.1.2
SetMultiThread sets the multi-threading for the Sitemap Parser. The multi-threading flag determines whether the parser should fetch URLs concurrently using goroutines. The function returns a pointer to the S structure to allow method chaining.
func (*S) SetRules ¶ added in v0.1.3
SetRules sets the rules patterns using the provided list of regex strings and compiles them into regex objects. Patterns longer than maxRegexPatternLength characters are rejected with a *ConfigError. Any errors encountered during compilation are recorded as *ConfigError values. The function returns a pointer to the S structure to allow method chaining.
func (*S) SetStrict ¶ added in v0.3.0
SetStrict enables or disables strict mode for URL validation. In strict mode, all URLs in sitemap <loc> elements must be absolute HTTP(S) URLs on the same host and protocol as the sitemap file, and must not exceed 2048 characters, as required by the sitemaps.org specification. In tolerant mode (default), relative URLs are resolved against the parent sitemap URL. The function returns a pointer to the S structure to allow method chaining.
func (*S) SetUserAgent ¶
SetUserAgent sets the user agent for the Sitemap Parser. The user agent is used for making HTTP requests when parsing and fetching URLs. It should be a string representing the user agent header value. The function returns a pointer to the S structure to allow method chaining.
type URL ¶
type URL struct {
Loc string `xml:"loc"`
LastMod *LastModTime `xml:"lastmod"`
ChangeFreq *URLChangeFreq `xml:"changefreq"`
Priority *float32 `xml:"priority"`
Images []Image `xml:"http://www.google.com/schemas/sitemap-image/1.1 image"`
News *News `xml:"http://www.google.com/schemas/sitemap-news/0.9 news"`
Videos []Video `xml:"http://www.google.com/schemas/sitemap-video/1.1 video"`
Hreflangs []AlternateLink `xml:"http://www.w3.org/1999/xhtml link"`
}
URL is a structure of <url> in <urlset>
type URLChangeFreq ¶ added in v0.3.0
type URLChangeFreq string
URLChangeFreq represents the frequency at which a URL should be crawled and indexed. Possible values are: "always", "hourly", "daily", "weekly", "monthly", "yearly", and "never".
const ( // ChangeFreqAlways represents the "always" value for URLChangeFreq. ChangeFreqAlways URLChangeFreq = "always" // ChangeFreqHourly represents the "hourly" value for URLChangeFreq. ChangeFreqHourly URLChangeFreq = "hourly" // ChangeFreqDaily represents the "daily" value for URLChangeFreq. ChangeFreqDaily URLChangeFreq = "daily" // ChangeFreqWeekly represents the "weekly" value for URLChangeFreq. ChangeFreqWeekly URLChangeFreq = "weekly" // ChangeFreqMonthly represents the "monthly" value for URLChangeFreq. ChangeFreqMonthly URLChangeFreq = "monthly" // ChangeFreqYearly represents the "yearly" value for URLChangeFreq. ChangeFreqYearly URLChangeFreq = "yearly" // ChangeFreqNever represents the "never" value for URLChangeFreq. ChangeFreqNever URLChangeFreq = "never" )
type ValidationError ¶ added in v0.9.0
type ValidationError struct {
// URL is the URL value being validated.
// May be empty for field-level errors where no specific URL is available.
URL string
// Err is the underlying validation error.
Err error
}
ValidationError is returned when a URL or field value fails validation. Callers can inspect URL to determine which value was rejected.
Example usage:
var valErr *sitemap.ValidationError
if errors.As(err, &valErr) {
fmt.Println("invalid URL:", valErr.URL)
}
func (*ValidationError) Error ¶ added in v0.9.0
func (e *ValidationError) Error() string
func (*ValidationError) Unwrap ¶ added in v0.9.0
func (e *ValidationError) Unwrap() error
Unwrap returns the underlying error, enabling errors.Is / errors.As traversal.
type Video ¶ added in v0.8.0
type Video struct {
ThumbnailLoc string `xml:"http://www.google.com/schemas/sitemap-video/1.1 thumbnail_loc"`
Title string `xml:"http://www.google.com/schemas/sitemap-video/1.1 title"`
Description string `xml:"http://www.google.com/schemas/sitemap-video/1.1 description"`
ContentLoc string `xml:"http://www.google.com/schemas/sitemap-video/1.1 content_loc"`
PlayerLoc string `xml:"http://www.google.com/schemas/sitemap-video/1.1 player_loc"`
Duration *int `xml:"http://www.google.com/schemas/sitemap-video/1.1 duration"`
ExpirationDate *LastModTime `xml:"http://www.google.com/schemas/sitemap-video/1.1 expiration_date"`
Rating *float32 `xml:"http://www.google.com/schemas/sitemap-video/1.1 rating"`
ViewCount *int `xml:"http://www.google.com/schemas/sitemap-video/1.1 view_count"`
PublicationDate *LastModTime `xml:"http://www.google.com/schemas/sitemap-video/1.1 publication_date"`
FamilyFriendly string `xml:"http://www.google.com/schemas/sitemap-video/1.1 family_friendly"`
Restriction *VideoRestriction `xml:"http://www.google.com/schemas/sitemap-video/1.1 restriction"`
Platform *VideoPlatform `xml:"http://www.google.com/schemas/sitemap-video/1.1 platform"`
RequiresSubscription string `xml:"http://www.google.com/schemas/sitemap-video/1.1 requires_subscription"`
Uploader *VideoUploader `xml:"http://www.google.com/schemas/sitemap-video/1.1 uploader"`
Live string `xml:"http://www.google.com/schemas/sitemap-video/1.1 live"`
Tags []string `xml:"http://www.google.com/schemas/sitemap-video/1.1 tag"`
}
Video is a structure of <video:video> in <url>, per the Google Video Sitemap extension. Reference: https://developers.google.com/search/docs/crawling-indexing/sitemaps/video-sitemaps
type VideoPlatform ¶ added in v0.8.0
type VideoPlatform struct {
Relationship string `xml:"relationship,attr"`
Value string `xml:",chardata"`
}
VideoPlatform is a structure of <video:platform> in <video:video>. It captures the element text and the required "relationship" attribute.
type VideoRestriction ¶ added in v0.8.0
type VideoRestriction struct {
Relationship string `xml:"relationship,attr"`
Value string `xml:",chardata"`
}
VideoRestriction is a structure of <video:restriction> in <video:video>. It captures the element text and the required "relationship" attribute.
type VideoUploader ¶ added in v0.8.0
VideoUploader is a structure of <video:uploader> in <video:video>. It captures the uploader name and the optional "info" URL attribute.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
advanced
command
|
|
|
atom
command
|
|
|
context
command
|
|
|
errors
command
|
|
|
getrandomurls
command
|
|
|
geturls
command
|
|
|
hreflang
command
|
|
|
httpclient
command
|
|
|
image
command
|
|
|
maxconcurrency
command
|
|
|
maxdepth
command
|
|
|
maxresponsesize
command
|
|
|
news
command
|
|
|
rss
command
|
|
|
rules
command
|
|
|
simple
command
|
|
|
strict
command
|
|
|
text
command
|
|
|
video
command
|