How to Use embed.FS for Embedded File Systems

Use the //go:embed directive with embed.FS to compile files directly into your Go binary for runtime access.

Use the embed.FS type to declare a variable that embeds a directory of files at compile time, making them accessible via the io/fs interface.

package main

import (
	"embed"
	"fmt"
	"io/fs"
)

//go:embed assets/*
var Content embed.FS

func main() {
	files, _ := fs.Glob(Content, "assets/*.txt")
	fmt.Println(files)
}

The //go:embed directive must precede the variable declaration and specify the path pattern relative to the source file.