Embedding external files
In the previous sections, you learned about a very interesting technique, but having external files to read can be problematic when deploying something to production, especially with Go, where one of its strong features is building a single executable. Fortunately, there is a package in Go called embed
that allows us to add external files to our final binary so that we need the original file when we develop, but we do not need to share this file with anybody else as it will be compiled and added to our final binary. Let’s see how this works.
Let’s imagine that you have a simple template file and want to use it on your web server:
mytemplate.html <h1>{{.Text}}</h1>
Let’s look at a small program that does exactly that, using what you’ve learned in the previous chapter:
package main import ( "html/template" "log" "net/http" ) func main() { ...