Documentation ¶
Overview ¶
Package embed allows for storing data resources in a virtual filesystem that gets compiled directly into the output program, eliminating the need to distribute data files with the application. This is especially useful for small web servers that need to deliver content files.
The data is gzipped to save space.
An external tool for generating output files can be found at http://github.com/cratonica/embedder
Author: Clint Caywood
http://github.com/cratonica/embed
Example ¶
package main import ( "fmt" "github.com/cratonica/embed" "os" ) func main() { // Note: This serialization part is implemented in a tool that can be found at http://github.com/cratonica/embedder // Create a resource map from files in a directory resourceMap, _ := embed.CreateFromFiles("/www/js") // Pack the files into a serializeable byte buffer packed, _ := embed.Pack(resourceMap) // Generate a .go file that we can include in our project goCode := embed.GenerateGoCode("main", "Scripts", packed) fout, _ := os.Open("scripts.go") fout.Write([]byte(goCode)) fout.Close() // ... // Now in our consumer program, we have a variable called "Scripts" var Scripts embed.PackedResourceMap // Unpack the resource map so we can use it scriptMap, _ := embed.Unpack(Scripts) // Pull out the resource we want jquery := scriptMap["jquery/jquery.min.js"] // Use the embedded file data fmt.Print(jquery) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateGoCode ¶
func GenerateGoCode(packageName string, varName string, data PackedResourceMap) string
Generates Go code containing the given data that can be included in the target project
Types ¶
type DeserializationError ¶
type DeserializationError struct {
// contains filtered or unexported fields
}
func NewDeserializationError ¶
func NewDeserializationError(what string, expected int, actual int) *DeserializationError
func (*DeserializationError) Error ¶
func (this *DeserializationError) Error() string
type PackedResourceMap ¶
type PackedResourceMap []byte
A compressed resource map that can be used for serialization
func Pack ¶
func Pack(data ResourceMap) (PackedResourceMap, error)
Packs the map of resource identifiers => []byte into a buffer. This process is reversed by calling Unpack.
type ResourceMap ¶
A mapping of resource identifiers to associated data
func CreateFromFiles ¶
func CreateFromFiles(path string) (ResourceMap, error)
Recursively packs all files in the given directory into a resource map. Directory delimiters are converted to Unix-style / regardless of the host operating system.
func Unpack ¶
func Unpack(data PackedResourceMap) (ResourceMap, error)
Reads a buffer generated by a call to Pack, returning the original map