favicon

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 20 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

favicon

Go Reference Dependencies

Find and download a website's favicon with Go.

go get go.lucor.dev/favicon

Pass a page URL to Download. URLs without a scheme use HTTPS; HTTP URLs are not supported.

import (
    "context"
    "net/url"

    "go.lucor.dev/favicon"
)

func discover(ctx context.Context) error {
    page, err := url.Parse("https://example.com")
    if err != nil {
        return err
    }

    result, err := favicon.Download(ctx, page, favicon.Options{})
    if err != nil {
        return err
    }

    if result.Icon.Raster != nil {
        // Use the decoded image.
    }
    return nil
}

The package supports PNG, JPEG, ICO, GIF, WebP, and SVG. SVG is returned as-is; it is not rendered or sanitized. Requests have a five-second timeout by default.

CLI

The diagnostic command saves raster icons as PNG and SVG icons as SVG:

go run ./cmd/favicon -debug -output favicon.png https://example.com

-output is optional. The default filename is favicon.png for raster icons and favicon.svg for SVG. Diagnostic logs go to standard error.

License

MIT. See LICENSE.

Documentation

Overview

Package favicon discovers, downloads, and decodes website favicons.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DownloadResult

type DownloadResult struct {
	Icon         Icon
	URL          string
	ETag         string
	LastModified string
	// NotModified indicates successful validation; Icon is empty in this case.
	NotModified bool
}

DownloadResult describes a downloaded icon and its HTTP cache validators.

func Download

func Download(ctx context.Context, u *url.URL, opts Options) (DownloadResult, error)

Download discovers and downloads the best icon linked from a page.

func DownloadURL

func DownloadURL(ctx context.Context, rawURL string, opts Options) (DownloadResult, error)

DownloadURL downloads and decodes an icon from a direct URL.

type Icon

type Icon struct {
	// Raster is the decoded image for raster formats.
	Raster image.Image
	// SVG is the original source for SVG images.
	SVG []byte
	// Format is the detected format, such as png, ico, or svg.
	Format string
	// Size is the raster image size and is zero for SVG.
	Size image.Point
}

Icon contains either a decoded raster image or the original SVG bytes. Exactly one field is set for a valid icon. SVG bytes are not sanitized.

func Decode

func Decode(data []byte) (Icon, error)

Decode validates and decodes bounded favicon data.

type Options

type Options struct {
	// Client is the HTTP client to use. A nil client uses a five-second timeout.
	Client *http.Client
	// UserAgent overrides the neutral default User-Agent.
	UserAgent string
	// ETag and LastModified validate the resource passed to DownloadURL.
	// Download discovers an icon and does not use these validators.
	ETag         string
	LastModified string
	// Logger is used for diagnostic logging. If nil, logging is disabled.
	Logger *slog.Logger
}

Options configures HTTP requests made while discovering or downloading an icon.

func WithLogger added in v0.2.0

func WithLogger(logger *slog.Logger) Options

WithLogger returns Options configured with the specified logger.

func (Options) WithLogger added in v0.2.0

func (o Options) WithLogger(logger *slog.Logger) Options

WithLogger returns a copy of Options configured with the specified logger.

Directories

Path Synopsis
cmd
favicon command

Jump to

Keyboard shortcuts

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