英文:
How to access resource files after the 'go' tool installed the executable?
问题
让我们假设运行 go install -v importpath
会构建一个可执行文件并将其安装到 $GOPATH/bin/program
。go
工具无法安装资源文件。当我运行 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:
-
write a simple script (awk should be sufficient) to convert any file into a
.go
file which just contains a single string constant and embed the file directly into the binary. This approach is used by the Camlistore project for example (camlistored/ui/fileembed.go). go-bindata does similar things. -
go-tour and many other projects are currently using the Import function of the
go/build
package to search through allsrc/
folders listed in$GOPATH
and$GOROOT
to find the correct path of the package sources. Example: http://code.google.com/p/go-tour/source/browse/gotour/local.go?r=996704f8ef9e63949b3bc4d94b613ec4a7b5d99a#53
答案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
> 在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: 没有那个文件或目录
英文:
> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论