如何在“go”工具安装可执行文件后访问资源文件?

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

How to access resource files after the 'go' tool installed the executable?

问题

让我们假设运行 go install -v importpath 会构建一个可执行文件并将其安装到 $GOPATH/bin/programgo 工具无法安装资源文件。当我运行 program 时,我希望它能够访问位于 $GOPATH/src/importpath 下的一些资源文件。

从安装的可执行文件中访问这些资源文件的最佳方法是什么?

英文:

Lets assume that running go install -v importpath builds an executable and installs it into $GOPATH/bin/program. The go tool is unable to install resource files. When I run the program, I would like it to access some resource files that are under $GOPATH/src/importpath.

What is the best way of accessing such resource files from the installed executable?

答案1

得分: 8

这个问题在go-nuts邮件列表上已经被问过多次了。目前,go工具没有直接分发附加资源的方法。然而,有两种可用的解决方法:

  • 编写一个简单的脚本(awk应该足够)将任何文件转换为一个.go文件,该文件只包含一个字符串常量,并将文件直接嵌入到二进制文件中。例如,Camlistore项目就使用了这种方法(camlistored/ui/fileembed.go)。go-bindata也做了类似的事情。

  • go-tour和许多其他项目目前正在使用go/build包的Import函数,在$GOPATH$GOROOT中列出的所有src/文件夹中搜索,以找到包源代码的正确路径。例如:http://code.google.com/p/go-tour/source/browse/gotour/local.go?r=996704f8ef9e63949b3bc4d94b613ec4a7b5d99a#53

英文:

This question has been asked multiple times on the go-nuts mailing list. The go tool doesn't offer a direct way to distribute additional resources at the moment. However, there are two workarounds available:

答案2

得分: 1

go install并不是为了实现你想要的功能而设计的。

如果你坚持使用go install,你可以将资源嵌入可执行文件中(在源代码中使用字节数组)。

另一个选项是使用一个部署脚本,该脚本运行go install,然后将资源复制到可执行文件所知道的位置。

如果你希望其他人能够安装你的程序,你应该使用针对目标操作系统的标准打包系统(例如Linux上的apt/rpm,Windows上的安装程序可执行文件,Mac上的.dmg文件等)。

英文:

go install is not designed to do what you want to do.

If you insist on using go install, you can embed the resources in the executable (byte arrays in source code).

Another option is to use a deploy script that runs go install and then copies the resources to a place known by your executable.

If you want your program to be installable by people other than you, you should use a packaging system that is standard on the os you're targeting (e.g. apt/rpm on Linux, an installer executable on Windows, .dmg file on Mac etc.)

答案3

得分: 0

GOPATH环境变量

> 在Unix上,该值是一个以冒号分隔的字符串。在Windows上,该值是一个以分号分隔的字符串。在Plan 9上,该值是一个列表。

因此,$GOPATH/bin/program$GOPATH/src/importpath不一定是有效的。例如,在Linux上,

$ GOPATH=$HOME/gopath:$HOME/go
$ cd $GOPATH/bin/go
bash: cd: /home/peter/gopath:/home/peter/go/bin/go: 没有那个文件或目录
英文:

GOPATH environment variable

> On Unix, the value is a colon-separated string. On Windows, the value
> is a semicolon-separated string. On Plan 9, the value is a list.

Therefore, $GOPATH/bin/program and $GOPATH/src/importpath are not necessarily valid. For example, on Linux,

$ GOPATH=$HOME/gopath:$HOME/go
$ cd $GOPATH/bin/go
bash: cd: /home/peter/gopath:/home/peter/go/bin/go: No such file or directory

huangapple
  • 本文由 发表于 2012年2月25日 19:03:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/9443418.html
匿名

发表评论

匿名网友

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

确定