Go Pack
Lightweight dependency-free embedding of static files into Go executables
Implementation
The implementation assumes the following binary file structure
executable + zip contents + zip size
The logic attempts to find a string size marker (32 bit) at the end of the Go executable. This marker is used
to calculate the offset of the zip contents from the end of the executable. The zip contents are optionally buffered
and used to access the static files in the zip contents or extracted to the directory containing the executable.
Usage
For the purposes of the included demo, the following steps generate the expected executable file format:
zip pack cmd/index.html
printf "%010d" `stat -f%z pack.zip` >> pack.zip
mv TheExecutable main.pack; cat main.pack pack.zip > TheExecutable
chmod +x TheExecutable
The API is basic:
package main
import "github.com/mlavergn/gopack/src/pack"
func main() {
pack := gopack.NewPack()
// A) extract to directory containting exectuable
pack.Extract()
// B) read from memory buffer
pack.Load()
// b1) string value
reader := pack.String("cmd/index.html")
// b2) pipe value (eg. http.resp)
reader := pack.Pipe("cmd/index.html")
ioutil.Copy(resp, reader)
}