Embed directive
Context
Section titled “Context”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.
Example
Section titled “Example”Embed a text file and serve it as a string.
Code example
Section titled “Code example”package main
import ( _ "embed" "fmt")
//go:embed greetings.txtvar greeting string
func main() { fmt.Print(greeting)}Assume greetings.txt contains:
Hello, embedded world!Output
Section titled “Output”Hello, embedded world!