It is often the case that a running Go program does not have access to the file system directories from which the the program was compiled.
The embed feature slurps up files in the package source code tree at build time and makes those files available at runtime.
Packages should embed files from the package source code tree that the package needs to access at runtime.
Here's an example: A package foo has has the source file foo.go. The package needs to access the file abc.txt in the same directory as foo.go.
If the current working directory is the source code code directory, then the package code can access the file using os.Open or anything that calls os.Open:
f, err := os.Open("abc.txt")
if err != nil {
// handle error
}
defer f.Close()
// do something with f
This code can fail. The current working directory at runtime is not necessarily the same as the package source code directory. It usually is not the same directory. The package source directory may not even be available to the running application.
In this case, the package should embed the file to ensure that the package can access the contents of the file:
//go:embed abc.txt
var abc []byte