README.md
1## bindata
2
3This package converts any file into managable Go source code. Useful for
4embedding binary data into a go program. The file data is optionally gzip
5compressed before being converted to a raw byte slice.
6
7It comes with a command line tool in the `go-bindata` sub directory.
8This tool offers a set of command line options, used to customize the
9output being generated.
10
11
12### Installation
13
14To install the library and command line program, use the following:
15
16 go get -u github.com/shuLhan/go-bindata/...
17
18
19### Usage
20
21Conversion is done on one or more sets of files. They are all embedded in a new
22Go source file, along with a table of contents and an `Asset` function,
23which allows quick access to the asset, based on its name.
24
25The simplest invocation generates a `bindata.go` file in the current
26working directory. It includes all assets from the `data` directory.
27
28 $ go-bindata data/
29
30To include all input sub-directories recursively, use the elipsis postfix
31as defined for Go import paths. Otherwise it will only consider assets in the
32input directory itself.
33
34 $ go-bindata data/...
35
36To specify the name of the output file being generated, we use the following:
37
38 $ go-bindata -o myfile.go data/
39
40Multiple input directories can be specified if necessary.
41
42 $ go-bindata dir1/... /path/to/dir2/... dir3
43
44
45The following paragraphs detail some of the command line options which can be
46supplied to `go-bindata`. Refer to the `testdata/out` directory for various
47output examples from the assets in `testdata/in`. Each example uses different
48command line options.
49
50To ignore files, pass in regexes using -ignore, for example:
51
52 $ go-bindata -ignore=\\.gitignore data/...
53
54### Accessing an asset
55
56To access asset data, we use the `Asset(string) ([]byte, error)` function which
57is included in the generated output.
58
59 data, err := Asset("pub/style/foo.css")
60 if err != nil {
61 // Asset was not found.
62 }
63
64 // use asset data
65
66
67### Debug vs Release builds
68
69When invoking the program with the `-debug` flag, the generated code does
70not actually include the asset data. Instead, it generates function stubs
71which load the data from the original file on disk. The asset API remains
72identical between debug and release builds, so your code will not have to
73change.
74
75This is useful during development when you expect the assets to change often.
76The host application using these assets uses the same API in both cases and
77will not have to care where the actual data comes from.
78
79An example is a Go webserver with some embedded, static web content like
80HTML, JS and CSS files. While developing it, you do not want to rebuild the
81whole server and restart it every time you make a change to a bit of
82javascript. You just want to build and launch the server once. Then just press
83refresh in the browser to see those changes. Embedding the assets with the
84`debug` flag allows you to do just that. When you are finished developing and
85ready for deployment, just re-invoke `go-bindata` without the `-debug` flag.
86It will now embed the latest version of the assets.
87
88
89### Lower memory footprint
90
91Using the `-nomemcopy` flag, will alter the way the output file is generated.
92It will employ a hack that allows us to read the file data directly from
93the compiled program's `.rodata` section. This ensures that when we call
94call our generated function, we omit unnecessary memcopies.
95
96The downside of this, is that it requires dependencies on the `reflect` and
97`unsafe` packages. These may be restricted on platforms like AppEngine and
98thus prevent you from using this mode.
99
100Another disadvantage is that the byte slice we create, is strictly read-only.
101For most use-cases this is not a problem, but if you ever try to alter the
102returned byte slice, a runtime panic is thrown. Use this mode only on target
103platforms where memory constraints are an issue.
104
105The default behaviour is to use the old code generation method. This
106prevents the two previously mentioned issues, but will employ at least one
107extra memcopy and thus increase memory requirements.
108
109For instance, consider the following two examples:
110
111This would be the default mode, using an extra memcopy but gives a safe
112implementation without dependencies on `reflect` and `unsafe`:
113
114```go
115func myfile() []byte {
116 return []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a}
117}
118```
119
120Here is the same functionality, but uses the `.rodata` hack.
121The byte slice returned from this example can not be written to without
122generating a runtime error.
123
124```go
125var _myfile = "\x89\x50\x4e\x47\x0d\x0a\x1a"
126
127func myfile() []byte {
128 var empty [0]byte
129 sx := (*reflect.StringHeader)(unsafe.Pointer(&_myfile))
130 b := empty[:]
131 bx := (*reflect.SliceHeader)(unsafe.Pointer(&b))
132 bx.Data = sx.Data
133 bx.Len = len(_myfile)
134 bx.Cap = bx.Len
135 return b
136}
137```
138
139
140### Optional compression
141
142When the `-nocompress` flag is given, the supplied resource is *not* GZIP
143compressed before being turned into Go code. The data should still be accessed
144through a function call, so nothing changes in the usage of the generated file.
145
146This feature is useful if you do not care for compression, or the supplied
147resource is already compressed. Doing it again would not add any value and may
148even increase the size of the data.
149
150The default behaviour of the program is to use compression.
151
152
153### Path prefix stripping
154
155The keys used in the `_bindata` map are the same as the input file name passed
156to `go-bindata`. This includes the path. In most cases, this is not desireable,
157as it puts potentially sensitive information in your code base. For this
158purpose, the tool supplies another command line flag `-prefix`. This accepts a
159[regular expression](https://github.com/google/re2/wiki/Syntax) string, which
160will be used to match a portion of the map keys and function names that should
161be stripped out.
162
163For example, running without the `-prefix` flag, we get:
164
165 $ go-bindata /path/to/templates/
166
167 _bindata["/path/to/templates/foo.html"] = path_to_templates_foo_html
168
169Running with the `-prefix` flag, we get:
170
171 $ go-bindata -prefix "/.*/some/" /a/path/to/some/templates/
172
173 _bindata["templates/foo.html"] = templates_foo_html
174
175
176### Build tags
177
178With the optional `-tags` flag, you can specify any go build tags that
179must be fulfilled for the output file to be included in a build. This
180is useful when including binary data in multiple formats, where the desired
181format is specified at build time with the appropriate tags.
182
183The tags are appended to a `// +build` line in the beginning of the output file
184and must follow the build tags syntax specified by the go tool.
185
186### Related projects
187
188[go-bindata-assetfs](https://github.com/elazarl/go-bindata-assetfs#readme) -
189implements `http.FileSystem` interface. Allows you to serve assets with `net/http`.
190
191