doubu7134 2019-09-14 21:34
浏览 152

gobuffalo / packr如何(为什么)在构建时运行?

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用