1vfsgen
2======
3
4[](https://travis-ci.org/shurcooL/vfsgen) [](https://godoc.org/github.com/shurcooL/vfsgen)
5
6Package vfsgen takes an http.FileSystem (likely at `go generate` time) and
7generates Go code that statically implements the provided http.FileSystem.
8
9Features:
10
11- Efficient generated code without unneccessary overhead.
12
13- Uses gzip compression internally (selectively, only for files that compress well).
14
15- Enables direct access to internal gzip compressed bytes via an optional interface.
16
17- Outputs `gofmt`ed Go code.
18
19Installation
20------------
21
22```bash
23go get -u github.com/shurcooL/vfsgen
24```
25
26Usage
27-----
28
29This code will generate an assets_vfsdata.go file with `var assets http.FileSystem = ...` that statically implements the contents of "assets" directory.
30
31```Go
32var fs http.FileSystem = http.Dir("assets")
33
34err := vfsgen.Generate(fs, vfsgen.Options{})
35if err != nil {
36 log.Fatalln(err)
37}
38```
39
40Then, in your program, you can use `assets` as any other [`http.FileSystem`](https://godoc.org/net/http#FileSystem), for example:
41
42```Go
43file, err := assets.Open("/some/file.txt")
44if err != nil { ... }
45defer file.Close()
46```
47
48```Go
49http.Handle("/assets/", http.FileServer(assets))
50```
51
52### `go generate` Usage
53
54vfsgen is great to use with go generate directives. The code invoking `vfsgen.Generate` can go in an assets_generate.go file, which can then be invoked via "//go:generate go run assets_generate.go". The input virtual filesystem can read directly from disk, or it can be more involved.
55
56By using build tags, you can create a development mode where assets are loaded directly from disk via `http.Dir`, but then statically implemented for final releases.
57
58For example, suppose your source filesystem is defined in a package with import path "example.com/project/data" as:
59
60```Go
61// +build dev
62
63package data
64
65import "net/http"
66
67// Assets contains project assets.
68var Assets http.FileSystem = http.Dir("assets")
69```
70
71When built with the "dev" build tag, accessing `data.Assets` will read from disk directly via `http.Dir`.
72
73A generate helper file assets_generate.go can be invoked via "//go:generate go run -tags=dev assets_generate.go" directive:
74
75```Go
76// +build ignore
77
78package main
79
80import (
81 "log"
82
83 "example.com/project/data"
84 "github.com/shurcooL/vfsgen"
85)
86
87func main() {
88 err := vfsgen.Generate(data.Assets, vfsgen.Options{
89 PackageName: "data",
90 BuildTags: "!dev",
91 VariableName: "Assets",
92 })
93 if err != nil {
94 log.Fatalln(err)
95 }
96}
97```
98
99Note that "dev" build tag is used to access the source filesystem, and the output file will contain "!dev" build tag. That way, the statically implemented version will be used during normal builds and `go get`, when custom builds tags are not specified.
100
101### `vfsgendev` Usage
102
103`vfsgendev` is a binary that can be used to replace the need for the assets_generate.go file.
104
105Make sure it's installed and available in your PATH.
106
107```bash
108go get -u github.com/shurcooL/vfsgen/cmd/vfsgendev
109```
110
111Then the "//go:generate go run -tags=dev assets_generate.go" directive can be replaced with:
112
113```
114//go:generate vfsgendev -source="example.com/project/data".Assets
115```
116
117vfsgendev accesses the source variable using "dev" build tag, and generates an output file with "!dev" build tag.
118
119### Additional Embedded Information
120
121All compressed files implement [`httpgzip.GzipByter` interface](https://godoc.org/github.com/shurcooL/httpgzip#GzipByter) for efficient direct access to the internal compressed bytes:
122
123```Go
124// GzipByter is implemented by compressed files for
125// efficient direct access to the internal compressed bytes.
126type GzipByter interface {
127 // GzipBytes returns gzip compressed contents of the file.
128 GzipBytes() []byte
129}
130```
131
132Files that have been determined to not be worth gzip compressing (their compressed size is larger than original) implement [`httpgzip.NotWorthGzipCompressing` interface](https://godoc.org/github.com/shurcooL/httpgzip#NotWorthGzipCompressing):
133
134```Go
135// NotWorthGzipCompressing is implemented by files that were determined
136// not to be worth gzip compressing (the file size did not decrease as a result).
137type NotWorthGzipCompressing interface {
138 // NotWorthGzipCompressing is a noop. It's implemented in order to indicate
139 // the file is not worth gzip compressing.
140 NotWorthGzipCompressing()
141}
142```
143
144Comparison
145----------
146
147vfsgen aims to be conceptually simple to use. The [`http.FileSystem`](https://godoc.org/net/http#FileSystem) abstraction is central to vfsgen. It's used as both input for code generation, and as output in the generated code.
148
149That enables great flexibility through orthogonality, since helpers and wrappers can operate on `http.FileSystem` without knowing about vfsgen. If you want, you can perform pre-processing, minifying assets, merging folders, filtering out files and otherwise modifying input via generic `http.FileSystem` middleware.
150
151It avoids unneccessary overhead by merging what was previously done with two distinct packages into a single package.
152
153It strives to be the best in its class in terms of code quality and efficiency of generated code. However, if your use goals are different, there are other similar packages that may fit your needs better.
154
155### Alternatives
156
157- [`go-bindata`](https://github.com/jteeuwen/go-bindata) - Reads from disk, generates Go code that provides access to data via a [custom API](https://github.com/jteeuwen/go-bindata#accessing-an-asset).
158- [`go-bindata-assetfs`](https://github.com/elazarl/go-bindata-assetfs) - Takes output of go-bindata and provides a wrapper that implements `http.FileSystem` interface (the same as what vfsgen outputs directly).
159- [`becky`](https://github.com/tv42/becky) - Embeds assets as string literals in Go source.
160- [`statik`](https://github.com/rakyll/statik) - Embeds a directory of static files to be accessed via `http.FileSystem` interface (sounds very similar to vfsgen); implementation sourced from [camlistore](https://camlistore.org).
161- [`go.rice`](https://github.com/GeertJohan/go.rice) - Makes working with resources such as HTML, JS, CSS, images and templates very easy.
162- [`esc`](https://github.com/mjibson/esc) - Embeds files into Go programs and provides `http.FileSystem` interfaces to them.
163- [`staticfiles`](https://github.com/bouk/staticfiles) - Allows you to embed a directory of files into your Go binary.
164- [`togo`](https://github.com/flazz/togo) - Generates a Go source file with a `[]byte` var containing the given file's contents.
165- [`fileb0x`](https://github.com/UnnoTed/fileb0x) - Simple customizable tool to embed files in Go.
166- [`embedfiles`](https://github.com/leighmcculloch/embedfiles) - Simple tool for embedding files in Go code as a map.
167- [`packr`](https://github.com/gobuffalo/packr) - Simple solution for bundling static assets inside of Go binaries.
168- [`rsrc`](https://github.com/akavel/rsrc) - Tool for embedding .ico & manifest resources in Go programs for Windows.
169
170Attribution
171-----------
172
173This package was originally based on the excellent work by [@jteeuwen](https://github.com/jteeuwen) on [`go-bindata`](https://github.com/jteeuwen/go-bindata) and [@elazarl](https://github.com/elazarl) on [`go-bindata-assetfs`](https://github.com/elazarl/go-bindata-assetfs).
174
175License
176-------
177
178- [MIT License](https://opensource.org/licenses/mit-license.php)