使用 golang 分发资源
Distributing resources with golang
我正在开发一个包含资源文件的库,特别是电子邮件模板。文件位于 <project_root>/templates/email.html
.
我很难从 .go
来源引用此文件,因为很难解析相对路径。到目前为止,我找到的选项是:
use $GOPATH
: 这带来了一些依赖管理工具可能会重新定义 $GOPATH
和 break
的问题
使用自定义环境变量:有效,但它会强制库的用户添加变量。
在字符串上内联模板:太丑了。
有没有人有更好的选择?我宁愿不为此使用第 3 方库,但如果您知道,请随时指出它,这样我就可以阅读源代码并了解他们是如何做到的。
谢谢
最常用的技术是嵌入资源,因此您只需分发二进制文件即可。
This list of Go libraries 有做资源嵌入的库列表:
- esc - Embeds files into Go programs and provides http.FileSystem interfaces to them.
- fileb0x - Simple tool to embed files in go with focus on "customization" and ease to use.
- go-bindata - Package that converts any file into managable Go source code.
- go-embed - Generates go code to embed resource files into your library or executable
- go-resources - Unfancy resources embedding with Go.
- go.rice - go.rice is a Go package that makes working with resources such as html,js,css,images
and templates very easy.
- statics - Embeds static resources into go files for single binary compilation + works with
http.FileSystem + symlinks.
- vfsgen - Generates a vfsdata.go file that statically implements the given virtual
filesystem.
我正在开发一个包含资源文件的库,特别是电子邮件模板。文件位于 <project_root>/templates/email.html
.
我很难从 .go
来源引用此文件,因为很难解析相对路径。到目前为止,我找到的选项是:
use
$GOPATH
: 这带来了一些依赖管理工具可能会重新定义$GOPATH
和 break 的问题
使用自定义环境变量:有效,但它会强制库的用户添加变量。
在字符串上内联模板:太丑了。
有没有人有更好的选择?我宁愿不为此使用第 3 方库,但如果您知道,请随时指出它,这样我就可以阅读源代码并了解他们是如何做到的。
谢谢
最常用的技术是嵌入资源,因此您只需分发二进制文件即可。
This list of Go libraries 有做资源嵌入的库列表:
- esc - Embeds files into Go programs and provides http.FileSystem interfaces to them.
- fileb0x - Simple tool to embed files in go with focus on "customization" and ease to use.
- go-bindata - Package that converts any file into managable Go source code.
- go-embed - Generates go code to embed resource files into your library or executable
- go-resources - Unfancy resources embedding with Go.
- go.rice - go.rice is a Go package that makes working with resources such as html,js,css,images and templates very easy.
- statics - Embeds static resources into go files for single binary compilation + works with http.FileSystem + symlinks.
- vfsgen - Generates a vfsdata.go file that statically implements the given virtual filesystem.