简体   繁体   English

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

[英]Golang — where should resources be kept

For quite some time, I have been wondering where to save resources for my go package and I just cannot find a correct answer. 很长一段时间以来,我一直想知道在哪里为go包节省资源,而我却找不到正确的答案。

Right now, resources are kept into 现在,资源被存放在

  • main/module1/resources 主要/模块1 /资源
  • main/module2/resources 主要/模块2 /资源

When I just use go run *.go from main, it works fine. 当我只使用main的go run *.go时,它可以正常工作。 When I run tests, however, the resources cannot be found because Go changes the current working directory to the corresponding module. 但是,当我运行测试时,找不到资源,因为Go将当前工作目录更改为相应的模块。 One workaround is to just os.Chdir('..') but doing that for every test seems kind of annoying. 一种解决方法是仅使用os.Chdir('..'),但对于每个测试都这样做有点烦人。 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). 我知道您不应因为并发而在go中使用全局变量,但是此变量对于并发读取是安全的(并且仅在启动期间写入)。 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): 后来,我发现了一个方法runtime.Caller来获取编译go文件的路径(如果有):

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. 现在,所有测试工作正常, go run *.go还会找到正确的资源。 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. 因为runtime.Caller使用的是编译期间使用的路径,所以我必须将资源放置在编译它们的位置上。 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. 这意味着当我在/home/matt3o12/go/src/domain.tld/matt3o12/mypackage构建二进制文件时,我必须将所有资源文件夹都放在服务器上的完全相同的路径中。

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). 我也尝试使用os.Executable但这是没有意义的,因为它只是指向内部包含编译后的可执行文件的临时路径(没有资源)。 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). 这可以工作,但是对于我运行的每个测试都使用它会很烦人(因为现在每次使用go test ./... go run *.go go test ./...go run *.go ,我都必须担心正确的资源路径)。
  • 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: 不是以下内容的重复:

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) . 最简单的方法可能是使用一些合理的默认值(例如,默认值为“。”或runtime.Caller(0)来配置资源的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM