简体   繁体   English

golang中资源应该放在哪里

[英]Where should resources be kept in golang

My application uses json configuration files and other resources.我的应用程序使用 json 配置文件和其他资源。 Where should I place them in my project hierarchy?我应该将它们放在我的项目层次结构中的什么位置? I could not find the answer in http://golang.org/doc/code.html (How to Write Go Code)我在http://golang.org/doc/code.html (如何编写 Go 代码)中找不到答案

Upd: The question is not about automatic distribution of resources with application but much simpler: Where should I keep my resources in project hierarchy? Upd:问题不在于应用程序自动分配资源,而更简单:我应该将资源保存在项目层次结构中的什么位置? Is there some standard place anyone expects them to be?有没有人期望他们成为标准的地方?

There is no single correct answer, nor are there any strong conventions assumed or enforced by any Go tooling at this time. 没有单一的正确答案,目前任何Go工具都没有任何强有力的约定。

Typically I start by assuming that the files I need are located in the same directory from where the program will be run. 通常我首先假设我需要的文件位于运行程序的同一目录中。 For instance, suppose I need conf.json for myprog.go ; 例如,假设我需要conf.json用于myprog.go ; then both of those files live together in the same directory and it works to just run something like 然后这两个文件一起存在于同一个目录中,它可以运行类似的东西

go build -o myprog && ./myprog

When I deploy the code, the myprog binary and conf.json live together on the server. 当我部署代码时, myprog二进制文件和conf.json在服务器上conf.json The run/supervisor script needs to cd to that directory and then run the program. 运行/管理程序脚本需要cd到该目录,然后运行该程序。

This also works when you have a lot of resources; 当你拥有大量资源时,这也有效; for instance, if you have a webserver with JS, CSS, and images, you just assume they're relative to cwd in the code and deploy the resource directories along with the server binary. 例如,如果您有一个包含JS,CSS和图像的Web服务器,您只需假设它们与代码中的cwd相关,并将资源目录与服务器二进制文件一起部署。

Another alternative to assuming a cwd is to have a -conf flag which the user can use to specify a configuration file. 假设cwd的另一种方法是使用-conf标志,用户可以使用该标志来指定配置文件。 I typically use this for distributing command-line tools and open-source server applications that require a single configuration file. 我通常使用它来分发需要单个配置文件的命令行工具和开源服务器应用程序。 You could even use an -assets flag or something to point to a whole tree of resource files, if you wanted. 如果需要,您甚至可以使用-assets标志或其他东西指向整个资源文件树。

Finally, one more approach is to not have any resource files. 最后,还有一种方法是没有任何资源文件。 go-bindata is a useful tool that I've used for this purpose -- it just encodes some data as bytes into a Go source file. go-bindata是我用于此目的的有用工具 - 它只是将一些数据作为字节编码到Go源文件中。 That way it's all baked into your binary. 这样它就全都融入了你的二进制文件。 I think this method is most useful when the resource data will rarely or never change, and is pretty small. 我认为当资源数据很少或永远不会改变时,这种方法最有用,并且非常小。 (Otherwise you're going to be shipping around huge binaries.) One (kind of silly) example of when I've used go-bindata in the past was for baking a favicon into a really simple server which didn't otherwise require any extra files besides the server binary. (否则你将围绕巨大的二进制文件运送。)过去我使用go-bindata的一个(有点愚蠢的)例子是将一个favicon烘焙到一个非常简单的服务器中,否则不需要任何服务器除服务器二进制文件外的额外文

For static resource it might be the most convenient solution to include them in the binary similar to resources in Java.对于静态资源,将它们包含在类似于 Java 中的资源的二进制文件中可能是最方便的解决方案。 Newer Go version, at least 1.18 are providing the //go:embed directive to include content:较新的 Go 版本,至少 1.18 提供了//go:embed 指令来包含内容:

import (
    _ "embed"
)

//go:embed myfile.txt
var myfile string

You can now use myfile in your code.您现在可以在代码中使用myfile Eg IntelliJ provides also support for this.例如,IntelliJ 也为此提供支持。

There are also other options to include the content, eg as binary or dynamically, see the link.还有其他选项可以包含内容,例如二进制或动态,请参阅链接。

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

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