Package providing a way to pack a full directory of files into a single go file in order to provide access to their content at runtime.
go install github.com/sebps/filepacker@v1.0.1
Executing filepacker command will pack a given source directory into a single .go file. All the directory files will be recursively wrapped. Any file will be made further accessible at run time combining a leading character "/" and the path of the file relatively to its initial source directory.
To pack a directory run the following command :
filepacker pack -s directory -t package -p resources
The result of the previous command execution will be a package.go file with the following content :
package resources // package name using flag -p
// Code generated by filepacker; DO NOT EDIT.
var repositoryInstance *repository
func init() {
repositoryInstance = &repository{
storage: make(map[string][]byte),
}
repositoryInstance.storage["/test.txt"] = []byte{116, 101, 115, 116}
}
type repository struct {
storage map[string][]byte
}
func (r *repository) Get(filename string) []byte {
return repositoryInstance.storage[filename]
}
At runtime, content of the files can be accessed from anywhere using the package getter as follows :
import (
filepackage "module_name/resources"
)
var fileBytes []byte := filepackage.Get('/<file_path>')