From the course: Go for Developers: Practical Techniques for Effective Coding

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Embedding files

Embedding files

- As we mentioned earlier, the FS package and the subsequent changes to the standard library were added to allow for the embedding of files such as HTML templates, JavaScript, and CSS into the final binary. As mentioned earlier, the FS package and its subsequent changes to the standard library were added to allow for the embedding of files such as HTML templates, JavaScript, and CSS into a binary. To use this feature, we can use the embed package in the Go Embed directive to define which file or files to embed. As per the documentation, we can either embed a single file as either a string or a slice of bytes. For our purposes, and more often than not, we will be embedding multiple files. So we'll need a file system implementation to hold the files. The embed FS type implements the FS interface and provides a read-only file system for the embedded files. So here we can define a global variable of type embed.FS and use the Go Embed directive to define the directories and files to embed.…

Contents