...

Package vfsgen

import "github.com/shurcooL/vfsgen"
Overview
Index
Examples
Subdirectories

Overview ▾

Package vfsgen takes an http.FileSystem (likely at `go generate` time) and generates Go code that statically implements the provided http.FileSystem.

Features:

- Efficient generated code without unneccessary overhead.

- Uses gzip compression internally (selectively, only for files that compress well).

- Enables direct access to internal gzip compressed bytes via an optional interface.

- Outputs `gofmt`ed Go code.

Example

This code will generate an assets_vfsdata.go file with `var assets http.FileSystem = ...` that statically implements the contents of "assets" directory. vfsgen is great to use with go generate directives. This code can go in an assets_gen.go file, which can then be invoked via "//go:generate go run assets_gen.go". The input virtual filesystem can read directly from disk, or it can be more involved.

Code:

var fs http.FileSystem = http.Dir("assets")

err := vfsgen.Generate(fs, vfsgen.Options{})
if err != nil {
    log.Fatalln(err)
}

func Generate

func Generate(input http.FileSystem, opt Options) error

Generate Go code that statically implements input filesystem, write the output to a file specified in opt.

type Options

Options for vfsgen code generation.

type Options struct {
    // Filename of the generated Go code output (including extension).
    // If left empty, it defaults to "{{toLower .VariableName}}_vfsdata.go".
    Filename string

    // PackageName is the name of the package in the generated code.
    // If left empty, it defaults to "main".
    PackageName string

    // BuildTags are the optional build tags in the generated code.
    // The build tags syntax is specified by the go tool.
    BuildTags string

    // VariableName is the name of the http.FileSystem variable in the generated code.
    // If left empty, it defaults to "assets".
    VariableName string

    // VariableComment is the comment of the http.FileSystem variable in the generated code.
    // If left empty, it defaults to "{{.VariableName}} statically implements the virtual filesystem provided to vfsgen.".
    VariableComment string
}

Subdirectories

Name Synopsis
..
cmd
vfsgendev vfsgendev is a convenience tool for using vfsgen in a common development configuration.
test Package test contains tests for virtual filesystem implementation generated by vfsgen.