Contents

Related

Go resource bundling

Notes on the installation and usage of pkger.

Installation done with

go get github.com/markbates/pkger/cmd/pkger

pkger works by bundling the resources with a code-generated pkg.go. The configuration of assets to be bundled is done by reflection at compile time and not direct configuration. This is done by replacing standard Go file operations with pkger proxy ones, such as:

type Pkger interface {
  Parse(p string) (Path, error)
  Current() (here.Info, error)
  Info(p string) (here.Info, error)
  Create(name string) (File, error)
  MkdirAll(p string, perm os.FileMode) error
  Open(name string) (File, error)
  Stat(name string) (os.FileInfo, error)
  Walk(p string, wf filepath.WalkFunc) error
  Remove(name string) error
  RemoveAll(path string) error
}
type File interface {
  Close() error
  Info() here.Info
  Name() string
  Open(name string) (http.File, error)
  Path() Path
  Read(p []byte) (int, error)
  Readdir(count int) ([]os.FileInfo, error)
  Seek(offset int64, whence int) (int64, error)
  Stat() (os.FileInfo, error)
  Write(b []byte) (int, error)
}

Example

Bundling a Go template file.

tmplFile, _ := pkger.Open("/templates/page.tmpl")
tmplBytes, _ := ioutil.ReadAll(tmplFile)
tmplString := string(tmplBytes)

tpl, err := template.New("page").Parse(tmplString)
_ = tpl.Execute(f, ...)

The bundling is simply done by running

pkger

and building as usual

go build