Embed Directive
// 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)
}Last updated