I'm developing a library that contains a resource file, specifically an email template. The file is on <project_root>/templates/email.html
.
I'm having a hard time referencing this file from the .go
sources, since it's hard to resolve a relative path. So far the options I found are:
use $GOPATH
: this comes with the issue that some dependency managemente tools may redefine $GOPATH
and break
use a custom env variable: works but it forces the user of the lib to add the variable.
inline the template on a string: it's ugly.
Does anyone have a better alternative? I'd rather not use 3rd party libs just for this but if you know one feel free to point it out so I can read the sources and see how they do it.
Thanks
The most common technique is to embed the resources so all you have to do is distribute the binary.
This list of Go libraries has a list of libraries that do resource embedding:
- 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.