Embed Directive
Use //go:embed to bundle a file into a string, byte slice, or embed.FS at build time.
The //go:embed directive is an instruction to the Go compiler to copy files into the program when it is built. Put it immediately before a string, byte slice or embed.FS variable. embed.FS lets the program read several embedded files by name.
This example expects greeting.txt beside the Go source file. The text is copied into the program, so it can be read when the program runs without opening a separate file on disk.
package main
import (
_ "embed"
"fmt"
)
//go:embed greeting.txt
var greeting string
func main() {
fmt.Print(greeting)
}In production
Embedded data cannot be changed while the program runs and makes the program file larger. An embedded file set can be used by functions that read files, including template.ParseFS. Embed only files that should be included with the program.