Embedding
Often, you will need to present to the user some complex text, maybe an HTML page, and it might be impractical to define the whole file as a string. You might read the file, as we learned in this chapter, and then use it as a template. You might want to display an image, again by opening and reading the file containing the image. One of the great features of Go is that even if you can build your application as a single binary, you will also have external dependencies that need to be distributed with your binary. Another issue is that reading from a file might be slow, so it would be great if we could embed files inside our Go application. This will allow us to just distribute one binary including all our assets. In the past, this required external libraries, but now Go includes a package called embed
that allows you to easily embed any file into your binary so that you do not need to share other dependencies. Let’s see an example of how we can do that.
In the next...