implant is a simple utility which allows data to be embedded directly
in golang applications (
implant is a synonym of
The expected use-case is that you have a HTTP-server, or similar
application which you wish to distribute as a single binary but which
needs some template files, HTML files, or other media.
Rather than distributing your binary along with a collection of files you instead embed the file contents in your application, and extract/use them at run-time.
implant allows you to do this, by generating a file
contains the contents of a particular directory hierarchy. At run-time you
can list available files, and extract the contents of specific ones.
If you're already setup for fetching & building golang applications you can download, or update, your local copy of the application via:
$ go get -u github.com/skx/implant $ go install github.com/skx/implant
This is equivalent to cloning our repository and building via:
$ go get . $ go build .
If you prefer you can fetch a binary release from our releases page.
Assuming that all the files you wish to embed are located within a single directory you would run:
$ implant -input data/ [-output static.go]
This would generate the file
static.go which contains the contents
of each file located beneath the
Further options are available to change the package-name, increase
verbosity and disable the automatic formatting of the code via
Please consult the output of
implant -help for details.
The generated file contains two functions to help you access your embedded data:
getResource( path string ) (byte, error)
- Return the content of a single embedded file.
- Returns a list of all the embedded resources in the your binary.
- The returned structure contains the original name of the file, the size, and the contents in encoded form.
- It is assumed you'll use
getResourceto get the original data, rather than trying to decode & decompress manually.
Sample usage of retrieving the content of the file
data/index.html would look like:
contents, err := getResource( "data/index.html" )
To save space the embedded file contents are compressed with
this results in binary-data so the compressed bytes are encoded via
encoding/hex. The use of the hex-representation does mean that some of
the compression is effectively wasted, but it is a reasonable trade-off.
The application uses itself to embed the template file which is written
static.go. In addition to that several of my small applications use
this library, for example:
You'll see in each case I've committed the generated
static.go file to the repository, which means users who don't need to change any of the resources aren't forced to install the tool.
I suggest a similar approach in your own deployments.
The repository contains a number of test-cases, they can can be executed via:
$ go test ./... ok github.com/skx/implant (cached) ok github.com/skx/implant/finder (cached)
To receive coverage details:
$ go test -coverprofile=tmp.t ./... && go tool cover -html=tmp.t && rm ./tmp.t