Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including template/html files in your go binary

Tags:

templates

go

Loving Go's built-in template libraries, currently I am just declaring the template as const string. How does one normally go about including larger more sophisticated template files? Ideally I prefer them to be inside the binary to simplify deployment.

like image 598
Jay Avatar asked May 18 '14 12:05

Jay


2 Answers

Embedding static files in 2021 has become a bit easier since the release of Go 1.16. The new release comes with a new package embed which provides a handy set of interface and methods to attach static file in go binaries

go version
# 1.16.x

# then
go doc embed

example in cks-cli software

like image 132
Abdennour TOUMI Avatar answered Nov 16 '22 02:11

Abdennour TOUMI


Here's a solution that fulfills the need and prevents the hardship described by mbazon:

https://godoc.org/github.com/go-bindata/go-bindata

It compiles to binary, but when you're in "debug" mode, it reads your static assets directly from the disk.

like image 37
ShawnMilo Avatar answered Nov 16 '22 01:11

ShawnMilo