How (why) can gobuffalo/packr run at build-time?

465 Views Asked by At

I am using gobuffalo/packr to embed template files into my golang binary. Consider this simplified example:

package main

import (
    "github.com/gobuffalo/packr"
)

func main() {
    foo, _ := packr.NewBox("./templates").FindString("text.tpl")
    println(foo)
}

According to the packr docs, one has to run the packr2 command in order to preprocess the template files so that they can be read and embedded by the regular go-compiler when it runs afterwards.

However, I do NOT have the packr repo or the binary itself installed. That import line in the golang code is the only reference to packr on my machine. And yet, running go install still produces a binary with templates correctly embedded.

Question: How can this be? What mechanism does packr employ here to be invoked at regular build time?

(Initially I assumed that packr might just use some init() magic to trigger the preprocessing. I also played around with go-generate annotations to reproduce that behaviour. Both experiments were unsuccesful - init() functions are not run at build time; and go-generate needs to be called explicitely. That leads me to believe, packr is using somewhat more delicate magic here.)

0

There are 0 best solutions below