Skip to main content

Embed Modules

Embed Module

This module is a super duper cool module in the sense that you are able to pack static files into your binary, rather than having your program relying on the actual file being present in your filesystem when it is ran.

Normally when you do deployment with a Go server, you would have the server binary, the static frontend files that is open and serve by the Go server. You can pack everything into a Docker image so the server that's running your web application can just spin up the container which will have everything. However, in Golang 1.16 you can directly embed your static frontend files into the binary so you do not need a docker image to pack your static files in. You can just embed it directly into your binary.

Embed directives

To get started with using the Embed module you would actually use the embed directives which instructs compiler at compile time what to do.

For example:

//go:embed $PATH
var content $WANTED_DATA_TYPE
  1. $PATH is the file or directory that you want to include. If you use dataa type embed.FS then you can put multiple files or multiple directories
  2. $WANTED_DATA_TYPE needs to be replace with one of the followings
    1. string = Accepts a single file and when the binary is built, it wil lread the content of that entire file into the variable as a string. If you use this you can only embed in ONE file. No multiple files or a directory
    2. []byte = Same as String, but it will be read in as []byte
    3. embed.FS = Using this data type you are allowed to embed multiple directories and even files. This struct implements the io/FS interface, which means that net/http package can use it as part of the handler