- Notifications
You must be signed in to change notification settings - Fork2
Go resource embedding library which doesn't require `go generate` at all
License
ichiban/assets
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Go resource embedding library which doesn't requirego generate
at all.
Resource Embedding in Go is cumbersome especially when it involves custom commands andgo generate
.assets
is another resource embedding library which doesn't require any of them.It extracts a zip file bundled with your binary and provides a file path to the contents.
- no custom commands
- no
go generate
- only requires
cat
andzip
whengo build
- uses local files when
go run
orgo test
- easily extendable interface
We assume you have a Go project and a directory namedassets
for resources in the project root.This directory name can be easily configured but for the sake of explanation we call the directoryassets
.
$ tree ..├── Makefile├── assets│ └── templates│ └── hello.tmpl└── main.go2 directories, 3 files
First go get the library.
$ go get github.com/ichiban/assets
Then import it.
import "github.com/ichiban/assets"
In Go files, we can get the locator which points toassets
directory by callingassets.New()
.Note that we'll need to close it in the end.
l,err:=assets.New()iferr!=nil {log.Fatalf("assets.New() failed: %v",err)}deferl.Close()log.Printf("assets: %s",l.Path)
When we build it and bundle with a resource zip file, the locator will point to the contents of the zip file which is extracted into a temporary directory.
To start with, we need to build the binary as usual.
$ mkdir -p bin $ go build -o bin/hello
Then, zip resources into a zip file.We need to enterassets
directory to make a proper zip file.
$ mkdir -p zip$ cd assets$ zip -r ../zip/assets.zip . adding: templates/ (stored 0%) adding: templates/hello.tmpl (stored 0%)$ cd ..
A proper zip file looks like this:
$ unzip -l zip/assets.zip Archive: zip/assets.zip Length Date Time Name--------- ---------- ----- ---- 0 11-05-2018 22:31 templates/ 14 11-05-2018 21:31 templates/hello.tmpl--------- ------- 14 2 files
Finally, we can bundle resources bycat
the binary and the zip file.Prepending an executable makes the zip offset values off by the size of the executable.We can fix the offsets byzip -A
(zip --adjust-sfx
).
$ cat bin/hello zip/assets.zip > bin/hello-bundled$ zip -A bin/hello-bundled Zip entry offsets appear off by 3345496 bytes - correcting...$ chmod +x bin/hello-bundled
Interestingly, the bundled binary is an executable and also a zip file.
$ unzip -l bin/hello-bundled Archive: bin/hello-bundled Length Date Time Name--------- ---------- ----- ---- 0 11-05-2018 22:31 templates/ 14 11-05-2018 21:31 templates/hello.tmpl--------- ------- 14 2 files
$ bin/hello-bundled 2018/11/30 16:22:13 assets: /var/folders/xt/6z9sk1dx1d734ltxxst16_h00000gn/T/hello-bundled882816570Hello, World!
This project is licensed under the MIT License - see theLICENSE.md file for details.
This project is inspired byZgok.
About
Go resource embedding library which doesn't require `go generate` at all