Embed files into a Go binary using the //go:embed directive to include static assets at compile time.
Use the //go:embed directive to embed files directly into your Go binary at compile time.
package main
import "embed"
//go:embed data.txt
var content []byte
func main() {
// content now holds the file bytes
}
For directories, use a wildcard pattern:
//go:embed assets/*
var assets embed.FS
The go:embed feature lets you pack files like images or configs directly inside your Go binary's executable file. Instead of needing separate files on the disk, the data travels with your code. It's like putting a letter inside an envelope so it never gets lost during shipping.