Skip to content

Embed directive

The //go:embed directive (introduced in Go 1.16) embeds files and directories into the binary at compile time. It is used for static assets, templates, and configuration files. The embed package provides types embed.FS.

Embed a text file and serve it as a string.

package main
import (
_ "embed"
"fmt"
)
//go:embed greetings.txt
var greeting string
func main() {
fmt.Print(greeting)
}

Assume greetings.txt contains:

Hello, embedded world!
Terminal window
Hello, embedded world!