Embed Directive
This Go program uses the embed
package to embed files and directories into the compiled binary. Let's go through the code with inline comments:
Explanation:
Embedding a Single File as a String and Byte Slice:
fileString
is a string variable that embeds the content of the file at "folder/single_file.txt" as a string.fileByte
is a byte slice variable that embeds the same content as a byte slice.
Embedding a Directory and Its Contents:
folder
is anembed.FS
variable that embeds the entire "folder" directory and its contents.
Reading and Printing Embedded Content:
The
main
function prints the content of the embedded string, byte slice, and reads and prints the content of specific files within the embedded directory.
Print Function:
A simple
print
function is defined for convenience to print the embedded content.
This program demonstrates how to embed files and directories into a Go program using the embed
package.
Last updated
Was this helpful?