staticfile

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2021 License: BSD-3-Clause Imports: 9 Imported by: 1

README

staticfile

GoDoc Go Report Card

This repository provides a tool to compile static data into a Go binary, and to access those data via a file-like interface.

The compiledata program generates a Go source file in the specified package that embeds the contents of the named file globs:

compiledata -pkg staticdata -out static.go data/*

The resulting file can be compiled into a package in the usual way. This tool can also be invoked from go generate rules.

In common use, the main package will blank import the static data package, and other packages access the files via the staticfile package:

import "github.com/creachadair/staticfile"

f, err := staticfile.Open("data/main.css")
...
defer f.Close()
doStuffWith(f)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustReadFile added in v0.1.0

func MustReadFile(path string) []byte

MustReadFile returns the full content of the specified static file or panics. It is intended for use during program initialization. Unlike ReadFile, this function does not delegate to the real filesystem.

func ReadFile added in v0.1.0

func ReadFile(path string) ([]byte, error)

ReadFile reads the complete contents of the specified file path. If path is not a registered static file path, ReadFile delegates to os.ReadFile.

func Register

func Register(path, data string)

Register the contents of a file under the given path. The path is cleaned by filepath.Clean. This function will panic if path == "" or if the cleaned path has previously been registered.

This function is meant to be used from generated code, and should not ordinarily be called directly by clients of the library.

Types

type File

type File interface {
	io.Reader
	io.ReaderAt
	io.Seeker
	io.Closer
}

File is the interface satisfied by files opened by the Open function. It is satisfied by the *os.File and *View types.

func Open

func Open(path string) (File, error)

Open opens the specified file path for reading. If path is not a registered static file path, Open delegates to os.Open. Otherwise, the concrete type of the result is *View.

type View added in v0.1.0

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

View is a read-only view of the contents of a static file. It implements the File interface.

func (*View) Close added in v0.1.0

func (*View) Close() error

Close implements io.Closer. This implementation never returns an error, and no resources are leaked if a *View is not closed.

func (*View) Read added in v0.1.0

func (v *View) Read(data []byte) (int, error)

Read implements the io.Reader interface.

func (*View) ReadAt added in v0.1.0

func (v *View) ReadAt(data []byte, off int64) (int, error)

ReadAt implements the io.ReaderAt interface.

func (*View) Seek added in v0.1.0

func (v *View) Seek(off int64, whence int) (int64, error)

Seek implements the io.Seeker interface.

func (*View) Size added in v0.1.0

func (v *View) Size() int64

Size reports the total unencoded size of the file contents, in bytes.

Directories

Path Synopsis
Binary compiledata generates Go source text containing encoded file data, for use with the github.com/creachadair/staticfile package.
Binary compiledata generates Go source text containing encoded file data, for use with the github.com/creachadair/staticfile package.
internal
bits
Package bits provides support routines for encoding and decoding data.
Package bits provides support routines for encoding and decoding data.

Jump to

Keyboard shortcuts

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