douhuzhi0907 2017-07-04 23:22
浏览 85

Golang-应该将资源保留在哪里

For quite some time, I have been wondering where to save resources for my go package and I just cannot find a correct answer.

Right now, resources are kept into

  • main/module1/resources
  • main/module2/resources

When I just use go run *.go from main, it works fine. When I run tests, however, the resources cannot be found because Go changes the current working directory to the corresponding module. One workaround is to just os.Chdir('..') but doing that for every test seems kind of annoying. Furthermore, sometimes the resources are loaded as the package initializes. That means there is a global map with all templates (and their corresponding names):

package module2

var myTemplates = map[string]template { "index": loadTemplateFromFile() }

I know you should not be using globals in go because of concurrency but this variable is safe for concurrent reads (and it is only written to during startup). So I put the templates into a global var so that they can be loaded on startup and some errors are caught during startup and the template are already in memory.

This means that I cannot change the working directory in time because the problem will already panic'd with an error telling me the file could not be found (this is the exact behavior I want).

Later, I discovered a method runtime.Caller to get the path from where the go files were compiled (if available):

base := "."
if _, filename, _, ok := runtime.Caller(0); ok {
    base = path.Join(path.Dir(filename), "..")
}
file := path.Join(base, "module1", "resources", ...)

Now, all tests work just fine and go run *.go also finds the correct resources. This method becomes an issue as soon as I start deploying my package. Because runtime.Caller uses the path which was used during compilation, I have to have the resources in the excact same location where I complied them. This means when I build the binary in /home/matt3o12/go/src/domain.tld/matt3o12/mypackage, I have to have all resource folders at the exact same path on the server.

I also tried to use os.Executable but this is pretty pointless because it just points to some temporary path with the compiled executable inside (and no resources). Unless I compile a test package and run it manually but this is very work intense.

I am also not looking to store the resources inside the binary (having a folder structure of the project resources is just fine).

I do have some ideas to solve this problem but I don't think any of them are optional:

  • Use a flag to point to the resource folder. This would work but using that for every test I run would be pretty annoying (because now everytime I use go test ./... or go run *.go, I have to worry about the proper resource path).
  • Use "magic" to figure out if tests are run and change the path accordingling. I would hate to use that because I would have to place those checks all over my code and I hate to have many "edge/special" cases.

This is not a duplicate of:

  • 写回答

1条回答 默认 最新

  • duanjuelu8874 2017-07-05 13:31
    关注

    There is no "should". You can put them wherever you see fit. You can embed them if you want. The easiest is probably to make the path to your resources configurable with some sane default (like defaulting to "." or runtime.Caller(0).

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog