Documentation ¶
Overview ¶
Goembed generates a file named "assets.generated.go" containing an encoded version of the contents of a directory.
The generated source code has no external import dependencies; it relies solely on the standard Go library.
The generated code provides a function that loads and returns the embedded assets, with the following signature:
func loadAssets() (map[string]string, error)
Given this "testdata" directory:
testdata/ |- index.html `- img `gopher.png
Then, after running "goembed testdata" and compiling the package, calling loadAssets will return a map like the following:
m["/index.html"] = "<html><head>..." m["/img/gopher.png"] = "\x89PNG\r\n\x1a\n..."
The paths will all begin at "/" and use forward slashes ("/") as path separators.
Goembed is useful in combination with "go generate" to bundle static assets in a program binary. For example, to embed all files under the "static" directory:
package main //go:generate embed static [...] $ go generate $ go build
Goembed supports encoding the data using the following algorithms:
- quote: quoted Go string
- hex: hex-encoded
- base64: base64-encoded
- zhex: zlib-compressed, hex-encoded
- zbase64: zlib-compressed, base64-encoded
Usage:
goembed [-package p] [-func f] [-o output] directory
The flags and their default values are:
-c=false use concurrent version of the chosen algorithm -e="quote" embedding algorithm -func="loadAssets" name of loading function -o="assets.generated.go" name of generated file -package="main" package of the generated source file (if $GOPACKAGE is set, such as when using "go generate", $GOPACKAGE takes precedence)
See also: package github.com/jeanfric/embedfs implements an http.FileSystem backed by a map[string]string, compatible directly with the map returned by loadAssets. See https://github.com/jeanfric/embedfs for more information.