feed

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package feed writes web feeds, Atom 1.0 (RFC 4287) and RSS 2.0, from one model: a Feed with its Entries. Atom is the preferred format; RSS is written for readers that know only RSS.

err := feed.Atom.Write(w, feed.Feed{
	ID:      "https://example.com/en/news/feed.atom",
	Title:   "News",
	Link:    "https://example.com/en/news",
	Self:    "https://example.com/en/news/feed.atom",
	Author:  "Example",
	Entries: entries,
})

Both formats are written from the same fields, so a Feed is checked against the elements both specifications require (see Validate) before anything is written. Times are written in RFC 3339 for Atom and RFC 1123 with a numeric zone for RSS; entry content is HTML, written escaped (Atom's type="html", RSS's content:encoded). The package uses the standard library only.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalid = errors.New("feed: invalid")

ErrInvalid is the error of a Feed that misses an element its formats require, or has one that is not valid (see Validate).

View Source
var Formats = []Format{Atom, RSS}

Formats are the formats a site offers a feed in, the preferred one, Atom, first.

Functions

func Language

func Language(locale string) string

Language is a locale as a BCP 47 language tag, as feeds write it: "nl_BE" becomes "nl-BE".

func TagURI

func TagURI(authority string, date time.Time, specific string) string

TagURI is a tag: URI (RFC 4151), an ID that stays the same when what it names moves, for Feed.ID and Entry.ID: authority is a domain name that was the publisher's on date, such as the site's host without its port, and specific tells the item apart from everything else the publisher named that day, such as "news/<post ID>".

feed.TagURI("example.com", created, "news/0199a3c1-…") // tag:example.com,2026-10-07:news/0199a3c1-…

Types

type Enclosure

type Enclosure struct {
	URL    string
	Type   string
	Length int64
}

Enclosure is a file of an entry, such as its image: its absolute address, its media type and its size in bytes, zero when it is not known.

type Entry

type Entry struct {
	// ID identifies the entry for good, an absolute IRI such as a tag: URI
	// (see TagURI): it stays the same when the entry changes or moves.
	// RSS writes it as the guid.
	ID string
	// Title is the entry's title, as plain text.
	Title string
	// Link is the absolute address of the entry's page.
	Link string
	// Summary is a short text about the entry, as plain text, such as an
	// excerpt. Optional.
	Summary string
	// Content is the entry's whole content as HTML, with absolute
	// addresses, as a reader shows it away from the site. Optional when
	// the entry has a Link.
	Content string
	// Published is when the entry was first published; Updated is when
	// it last changed in a way that matters to readers, the Published
	// time when zero.
	Published time.Time
	Updated   time.Time
	// Categories are the names of the entry's categories.
	Categories []string
	// Author is the name of the entry's author; empty is the feed's.
	Author string
	// Image is an image of the entry, such as a post's featured image,
	// written as an enclosure. Optional.
	Image Enclosure
}

Entry is one item of a feed, such as a post.

type Feed

type Feed struct {
	// ID identifies the feed for good: an absolute IRI that does not change
	// when the feed moves, such as a tag: URI (see TagURI), or the address
	// of the feed when it stays where it is. Atom only.
	ID string
	// Title is the feed's name as readers list it.
	Title string
	// Subtitle says what the feed is about, Atom's subtitle; RSS's
	// description, which RSS requires, is the Title when it is empty.
	Subtitle string
	// Link is the absolute address of the web page the feed belongs to.
	Link string
	// Self is the absolute address of the feed in the format written, for
	// Atom's link rel="self" and RSS's atom:link; the writer of the
	// document sets it per format. Optional.
	Self string
	// Updated is when the feed last changed; zero is the newest Updated of
	// its entries.
	Updated time.Time
	// Language is the language of the feed, a BCP 47 tag such as "nl" or
	// "nl-BE"; a locale written with an underscore, such as "nl_BE", is
	// written with a hyphen. Atom's xml:lang, RSS's language. Optional.
	Language string
	// Author is the name of who publishes the feed, such as the site's
	// name. Atom requires an author for every entry: Author is the author
	// of entries without one of their own.
	Author string
	// Entries are the feed's items, newest first.
	Entries []Entry
}

Feed is a feed: what it is and its entries, newest first.

func (Feed) LastModified

func (f Feed) LastModified() time.Time

LastModified is when the feed's content last changed: Updated, or the newest time an entry changed. A server's Last-Modified header may be later, such as when an entry was removed, which leaves no newer time in the feed.

func (Feed) Validate

func (f Feed) Validate() error

Validate checks that the feed has what both formats require: an ID that is an absolute IRI, a Title, an absolute Link and a time it was updated, its own or one of an entry, and an Author unless every entry has one. Every entry needs an absolute ID, a Title, a time it was published or updated, and an absolute Link or Content. Addresses that are given must be absolute, an image needs its type, and the Language must be a language tag. The error wraps ErrInvalid and names each problem.

type Format

type Format string

Format is a feed format, also the extension of its address, such as feed.atom: Atom or RSS.

const (
	Atom Format = "atom"
	RSS  Format = "rss"
)

The formats a feed is written in.

func (Format) ContentType

func (f Format) ContentType() string

ContentType is the Content-Type of a feed in the format: application/atom+xml or application/rss+xml, in UTF-8.

func (Format) MediaType

func (f Format) MediaType() string

MediaType is the media type of the format without parameters, as a <link rel="alternate" type="…"> names it: application/atom+xml or application/rss+xml.

func (Format) Write

func (f Format) Write(w io.Writer, feed Feed) error

Write writes feed in the format, after Validate; a feed that is not valid is not written.

Jump to

Keyboard shortcuts

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