drnysdnnb2909701 2019-07-26 02:01
浏览 26
已采纳

如何在Go中处理支持包中文件的路径?

Go program with the following structure:-

├── app.go
├── bin
│   └── run.go
├── config
│   └── Config.go
└── package1
    ├── package1_file.go
    └── tmpl
        └── template.tmpl

Now, in package1_file.go, I've accessed template.tmpl via relative path like:

t, err := template.ParseFiles("./tmpl/template.tmpl")

When I run tests, tests are able to run successfully because my guess is, Go changes the current working directory when running tests for packages. go tests -v ./...

However, when I run (go build -o app && ./app) the program from the root folder, I get error complaining that file doesn't exist.

Error compiling template: open ./tmpl/template.tmpl: no such file or directory

It starts working when I change the path to package2/tmpl/template.tmpl.

The code outside package2 has nothing to do with this template file so I don't want to expose it as a parameter to a function while exposing package2. What are my options?

What is the right way to target support files like these?

  • 写回答

2条回答 默认 最新

  • dtlc84438 2019-07-26 13:59
    关注

    You're operating under some mistaken assumptions here - primarily that the project source code or directory structure are in any way relevant at runtime. They aren't.

    A Go program compiles to a single binary file that can be executed anywhere, without the source, without Go installed - just the binary. You need to consider that any time you have any kind of files in your project that you will need at runtime:

    • You need to decide how these files will be located:
      • You can mandate a path, either relative to CWD at time of execution, or absolute (but you shouldn't)
      • You can accept the path as a runtime parameter, by CLI, environment variable, config file, etc.
      • You can embed them into the binary itself using one of the many packages available (seriously - so many that a Google search for go embed static files turns up not only several libraries, but several articles comparing various libraries)
    • You need to decide how the whole thing will be packaged:
      • You could zip/tar/whatever the resources alongside the binary
      • You could zip/tar/whatever the resources and binary all together
      • As above, you could embed them into the binary as a single file

    There are some choices you'll have to make as to how you want to handle this but the key takeaway is don't assume the path in your source code will be at all relevant at runtime. Parameterize at least the root path to the resources so that your code will work wherever they may be, and then your tests can pass in an appropriate path to use for testing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信