• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

go-bindata/H09-Feb-2018-

testdata/H09-Feb-2018-

CONTRIBUTING.mdH A D09-Feb-20183.5 KiB

LICENSEH A D09-Feb-2018169

MakefileH A D09-Feb-201823

README.mdH A D09-Feb-20186.5 KiB

asset.goH A D09-Feb-2018464

bytewriter.goH A D09-Feb-2018668

config.goH A D09-Feb-20186.5 KiB

convert.goH A D09-Feb-20186 KiB

convert_test.goH A D09-Feb-20182.7 KiB

debug.goH A D09-Feb-20181.9 KiB

doc.goH A D09-Feb-20184.9 KiB

release.goH A D09-Feb-20187.1 KiB

restore.goH A D09-Feb-20181.4 KiB

stringwriter.goH A D09-Feb-2018551

toc.goH A D09-Feb-20185.4 KiB

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/jteeuwen/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
156passed to `go-bindata`. This includes the path. In most cases, this is not
157desireable, as it puts potentially sensitive information in your code base.
158For this purpose, the tool supplies another command line flag `-prefix`.
159This accepts a portion of a path name, which should be stripped off from
160the map keys and function names.
161
162For example, running without the `-prefix` flag, we get:
163
164	$ go-bindata /path/to/templates/
165
166	_bindata["/path/to/templates/foo.html"] = path_to_templates_foo_html
167
168Running with the `-prefix` flag, we get:
169
170	$ go-bindata -prefix "/path/to/" /path/to/templates/
171
172	_bindata["templates/foo.html"] = templates_foo_html
173
174
175### Build tags
176
177With the optional `-tags` flag, you can specify any go build tags that
178must be fulfilled for the output file to be included in a build. This
179is useful when including binary data in multiple formats, where the desired
180format is specified at build time with the appropriate tags.
181
182The tags are appended to a `// +build` line in the beginning of the output file
183and must follow the build tags syntax specified by the go tool.
184
185### Related projects
186
187[go-bindata-assetfs](https://github.com/elazarl/go-bindata-assetfs#readme) -
188implements `http.FileSystem` interface. Allows you to serve assets with `net/http`.
189
190