'go'工具安装可执行文件后如何访问资源文件? [英] How to access resource files after the 'go' tool installed the executable?
问题描述
假设运行 go install -v importpath
生成一个可执行文件并将其安装到 $ GOPATH / bin / program
。 go
工具无法安装资源文件。当我运行程序
时,我希望它能够访问 $ GOPATH / src / importpath
下的一些资源文件, 。
从安装的可执行文件访问这些资源文件的最佳方式是什么?
此问题已被要求多个 <一个href =https://groups.google.com/d/topic/golang-nuts/XksK4klJ0Dg/discussion =nofollow>次在go-nuts邮件列表中。 编写一个简单的脚本(awk应该足以)将任何文件转换为 go-tour和许多其他项目目前正在使用导入 go
工具目前不提供直接分发更多资源的方式。但是,有两种解决方法可用:
.go
文件,它只包含一个字符串常量,并将文件直接嵌入到二进制文件中。例如, Camlistore 项目使用此方法( camlistored / ui / fileembed.go )。 go-bindata 做类似的事情。
go / build
包的功能,用于搜索 $ GOPATH中列出的所有
和 src /
文件夹 $ GOROOT
来查找包源的正确路径。示例: http://代码.google.com / p / go-tour / source / browse / gotour / local.go?r = 996704f8ef9e63949b3bc4d94b613ec4a7b5d99a#53
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?
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
这篇关于'go'工具安装可执行文件后如何访问资源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!