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:

// Importing the embed package.
import (
	"embed"
)

//go:embed folder/single_file.txt
var fileString string // Embedding the content of a single text file as a string.

//go:embed folder/single_file.txt
var fileByte []byte // Embedding the content of a single text file as a byte slice.

//go:embed folder/single_file.txt
//go:embed folder/*.hash
var folder embed.FS // Embedding a directory and its contents.

func main() {
	// Printing the content of the embedded string.
	print(fileString)
	// Printing the content of the embedded byte slice.
	print(string(fileByte))

	// Reading and printing the content of a specific file ("folder/file1.hash") from the embedded directory.
	content1, _ := folder.ReadFile("folder/file1.hash")
	print(string(content1))

	// Reading and printing the content of another file ("folder/file2.hash") from the embedded directory.
	content2, _ := folder.ReadFile("folder/file2.hash")
	print(string(content2))
}

// print function for convenience.
func print(s string) {
	println(s)
}

Explanation:

  1. 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.

  2. Embedding a Directory and Its Contents:

    • folder is an embed.FS variable that embeds the entire "folder" directory and its contents.

  3. 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.

  4. 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