Skip to content

cratonica/embed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

embed

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 here

View the full documentation here

Example

// 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)

About

Embed data resources into a Go application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages