Go 程序具有以下结构:-
├── app.go
├── bin
│ └── run.go
├── config
│ └── Config.go
└── package1
├── package1_file.go
└── tmpl
└── template.tmpl
现在,在 中package1_file.go,我通过相对路径访问了 template.tmpl,例如:
t, err := template.ParseFiles("./tmpl/template.tmpl")
当我运行测试时,测试能够成功运行,因为我的猜测是,Go 在运行包测试时更改了当前工作目录。go tests -v ./...
但是,当我(go build -o app && ./app)从根文件夹运行该程序时,出现错误,抱怨该文件不存在。
Error compiling template: open ./tmpl/template.tmpl: no such file or directory
当我将路径更改为 时它开始工作package2/tmpl/template.tmpl。
外部的代码package2与此模板文件无关,因此我不想在公开package2. 我有什么选择?
定位此类支持文件的正确方法是什么?
RISEBY
米琪卡哇伊
相关分类