go-assets: github.com/jessevdk/go-assets Index | Examples | Files

package assets

import "github.com/jessevdk/go-assets"

go-assets is a simple embedding asset generator and consumer library for go. The main use of the library is to generate and embed small in-memory file systems ready to be integrated in webservers or other services which have a small amount of assets used at runtime. This is great for being able to do single binary deployments with assets.

The Generator type can be used to generate a go file containing an in-memory file tree from files and directories on disk. The file data can be optionally compressed using gzip to reduce file size. Afterwards, the generated file can be included into your application and the assets can be directly accessed without having to load them from disk. The generated assets variable is of type FileSystem and implements the os.FileInfo and http.FileSystem interfaces so that they can be directly used with http.FileHandler.

Index

Examples

Package Files

assets.go file.go filesystem.go generate.go

type File Uses

type File struct {
    // The full asset file path
    Path string

    // The asset file mode
    FileMode os.FileMode

    // The asset modification time
    Mtime time.Time

    // The asset data. Note that this data might be in gzip compressed form.
    Data []byte
    // contains filtered or unexported fields
}

An asset file.

func (*File) Close Uses

func (f *File) Close() error

func (*File) IsDir Uses

func (f *File) IsDir() bool

func (*File) ModTime Uses

func (f *File) ModTime() time.Time

func (*File) Mode Uses

func (f *File) Mode() os.FileMode

func (*File) Name Uses

func (f *File) Name() string

func (*File) Read Uses

func (f *File) Read(data []byte) (int, error)

func (*File) Readdir Uses

func (f *File) Readdir(count int) ([]os.FileInfo, error)

func (*File) Seek Uses

func (f *File) Seek(offset int64, whence int) (int64, error)

func (*File) Size Uses

func (f *File) Size() int64

func (*File) Stat Uses

func (f *File) Stat() (os.FileInfo, error)

func (*File) Sys Uses

func (f *File) Sys() interface{}

type FileSystem Uses

type FileSystem struct {
    // A map of directory paths to the files in those directories.
    Dirs map[string][]string

    // A map of file/directory paths to assets.File types.
    Files map[string]*File

    // Override loading assets from local path. Useful for development.
    LocalPath string
}

An in-memory asset file system. The file system implements the http.FileSystem interface.

func NewFileSystem Uses

func NewFileSystem(dirs map[string][]string, files map[string]*File, localPath string) *FileSystem

func (*FileSystem) NewFile Uses

func (f *FileSystem) NewFile(path string, filemode os.FileMode, mtime time.Time, data []byte) *File

func (*FileSystem) Open Uses

func (f *FileSystem) Open(p string) (http.File, error)

Implementation of http.FileSystem

type Generator Uses

type Generator struct {
    // The package name to generate assets in,
    PackageName string

    // The variable name containing the asset filesystem (defaults to Assets),
    VariableName string

    // Strip the specified prefix from all paths,
    StripPrefix string
    // contains filtered or unexported fields
}

An asset generator. The generator can be used to generate an asset go file with all the assets that were added to the generator embedded into it. The generated assets are made available by the specified go variable VariableName which is of type assets.FileSystem.

Code:

g := Generator{}

if err := g.Add("."); err != nil {
    panic(err)
}

// This will write a go file to standard out. The generated go file
// will reside in the g.PackageName package and will contain a
// single variable g.VariableName of type assets.FileSystem containing
// the whole file system.
g.Write(os.Stdout)

func (*Generator) Add Uses

func (x *Generator) Add(p string) error

Add a file or directory asset to the generator. Added directories will be recursed automatically.

func (*Generator) Write Uses

func (x *Generator) Write(wr io.Writer) error

Write the asset tree specified in the generator to the given writer. The written asset tree is a valid, standalone go file with the assets embedded into it.

Package assets imports 11 packages (graph) and is imported by 18 packages. Updated 2018-06-29. Refresh now. Tools for package owners.