Bindata
This package converts any file into managable Go source code. Useful for
embedding binary data into a go program. The file data is optionally gzip
compressed before being converted to a raw byte slice.
It comes with a command line tool in the bindata
sub directory.
This tool offers a set of command line options, used to customize the
output being generated.
Installation
To install the library and command line program, use the following:
go install code.afis.me/go-nano-services/modules/bindata
Usage
Conversion is done on one or more sets of files. They are all embedded in a new
Go source file, along with a table of contents and an Asset
function,
which allows quick access to the asset, based on its name.
The simplest invocation generates a bindata.go
file in the current
working directory. It includes all assets from the data
directory.
bindata data/
To include all input sub-directories recursively, use the elipsis postfix
as defined for Go import paths. Otherwise it will only consider assets in the
input directory itself.
bindata data/...
To specify the name of the output file being generated, we use the following:
bindata -o myfile.go data/
Multiple input directories can be specified if necessary.
bindata dir1/... /path/to/dir2/... dir3
To ignore files, pass in regexes using -ignore, for example:
bindata -ignore=\\.gitignore data/...
Accessing an asset
To access asset data, we use the Asset(string) ([]byte, error)
function which
is included in the generated output.
data, err := Asset("pub/style/foo.css")
if err != nil {
// Asset was not found.
}
// use asset data
Serve assets with net/http
With the -fs flag, bindata will add an AssetFile(string) method returning an http.FileSystem interface:
r := mux.NewRouter().StrictSlash(true)
r.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(AssetFile(""))))
r.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(AssetFile("assets/dist"))))
#cloning and modif from
https://github.com/go-bindata/go-bindata
Debug vs Release builds
When invoking the program with the -debug
flag, the generated code does
not actually include the asset data. Instead, it generates function stubs
which load the data from the original file on disk. The asset API remains
identical between debug and release builds, so your code will not have to
change.
This is useful during development when you expect the assets to change often.
The host application using these assets uses the same API in both cases and
will not have to care where the actual data comes from.
An example is a Go webserver with some embedded, static web content like
HTML, JS and CSS files. While developing it, you do not want to rebuild the
whole server and restart it every time you make a change to a bit of
javascript. You just want to build and launch the server once. Then just press
refresh in the browser to see those changes. Embedding the assets with the
debug
flag allows you to do just that. When you are finished developing and
ready for deployment, just re-invoke bindata
without the -debug
flag.
It will now embed the latest version of the assets.