Lesson 42 of 60 · Go
Embedding Files – `embed` Package (Go 1.16+)
Duration: 15 minutes
Compile‑time Assets
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
embed lets you embed static files directly into the binary.
import "embed"
//go:embed templates/*.html
var tmplFS embed.FS
func main() {
files, _ := tmplFS.ReadDir("templates")
for _, f := range files { fmt.Println(f.Name()) }
}
You can also embed a single file as a string:
//go:embed version.txt
var version string