在Go项目中组织资产文件

huangapple go评论81阅读模式
英文:

Organizing asset files in a Go project

问题

我有一个项目,其中包含一个文件夹来管理文件模板,但是Go似乎没有提供对非Go代码项目文件的支持。该项目本身编译为可执行文件,但它需要知道模板文件夹的位置才能正确运行。目前,我在$GOPATH/src//templates中进行搜索,但这对我来说感觉有点像一个hack,因为如果我决定重命名包或将其托管到其他地方,它就会失效。

我已经进行了一些搜索,看起来有很多人都对能够通过将它们嵌入到最终的二进制文件中来“编译”资产文件感兴趣,但我对这种方法感到不确定。

有什么想法吗?

英文:

I have a project that contains a folder to manage file templates, but it doesn't look like Go provides any support for non-Go-code project files. The project itself compiles to an executable, but it needs to know where this template folder is in order to operate correctly. Right now I do a search for $GOPATH/src/<templates>/templates, but this feels like kind of a hack to me because it would break if I decided to rename the package or host it somewhere else.

I've done some searching and it looks like a number of people are interested in being able to "compile" the asset files by embedding them in the final binary, but I'm not sure how I feel about this approach.

Any ideas?

答案1

得分: 2

要么选择一个用户预期将支持数据放在其中的路径(或路径列表)(/usr/local/share/myapp,...),要么将其编译到二进制文件中。

这取决于您计划如何分发程序。作为一个软件包?带有安装程序?

我喜欢我的大多数程序只有一个文件要部署,我只需要包含一些模板,所以我这样做。

我有一个示例,使用go-bindata构建html模板的Makefile,但如果我使用'devel'标志构建,它将在运行时读取文件,以便更容易进行开发。

英文:

Either pick a path (or a list of paths) that users are expected to put the supporting data in (/usr/local/share/myapp, ...) or just compile it into the binary.

It depends on how you are planning to distribute the program. As a package? With an installer?

Most of my programs I enjoy just having a single file to deploy and I just have a few templates to include, so I do that.

I have an example using go-bindata where I build the html template with a Makefile, but if I build with the 'devel' flag it will read the file at runtime instead to make development easier.

答案2

得分: 1

我可以想到两个选项,使用cwd标志,或者从cwd和arg 0推断:

-cwd path/to/assets

path/to/exe -cwd=$(path/to/exe/assets)

在内部,可执行文件将更改到cwd指向的位置,然后它可以在整个应用程序中使用相对路径。这样做的好处是用户可以在不重新编译程序的情况下更改资产。

我对配置文件也是这样做的。基本上顺序如下:

  • 处理cmd参数,查找-cwd变量(默认为空)
  • 更改到-cwd
  • 解析配置文件
  • 重新解析cmd参数,覆盖配置文件中的设置

我不确定您的应用程序有多少参数,但我发现这非常有用,特别是因为Go没有一个标准的打包工具来编译这些资产。

从arg 0推断

另一个选项是使用第一个参数并获取可执行文件的路径。类似这样:

here := path.Dir(os.Args[0])
if !path.IsAbs(os.Args[0]) {
    here = path.Join(os.Getwd(), here)
}

这将为您提供可执行文件所在的路径。如果您可以确保用户不会移动此文件而不移动其余的资产,您可以使用此选项,但我发现使用上述-cwd的想法更加灵活,因为用户可以将可执行文件放在系统的任何位置,然后只需指向资产。

最好的选择可能是两者的混合。如果用户没有提供-cwd标志,他们可能没有移动任何内容,因此从arg 0和cwd推断。cwd标志将覆盖此设置。

英文:

I can think of two options, use a cwd flag, or infer from cwd and arg 0:

-cwd path/to/assets

path/to/exe -cwd=$(path/to/exe/assets)

Internally, the exectable would chdir to wherever cwd points to, and then it can use relative paths throughout the application. This has the added benefit that the user can change the assets without having to recompile the program.

I do this for config files. Basically the order goes:

  • process cmd arguments, looking for a -cwd variable (it defaults to empty)
  • chdir to -cwd
  • parse config file
  • reparse cmd arguments, overwriting the settings in the config file

I'm not sure how many arguments your app has, but I've found this to be very useful, especially since Go doesn't have a standard packaging tool that will compile these assets in.

infer from arg 0

Another option is to use the first argument and get the path to the executable. Something like this:

here := path.Dir(os.Args[0])
if !path.IsAbs(os.Args[0]) {
    here = path.Join(os.Getwd(), here)
}

This will get you the path to where the executable is. If you're guaranteed the user won't move this without moving the rest of your assets, you can use this, but I find it much more flexible to use the above -cwd idea, because then the user can place the executable anywhere on their system and just point it to the assets.

The best option would probably be a mixture of the two. If the user doesn't supply a -cwd flag, they probably haven't moved anything, so infer from arg 0 and the cwd. The cwd flag overrides this.

huangapple
  • 本文由 发表于 2013年7月10日 02:31:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/17555618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定