← All notes

The //go:embed directive compiles files into a Go binary as a string, []byte or embed.FS

Answer: Import embed and put //go:embed PATTERN directly above a variable of type string, []byte or embed.FS. The go command then includes those files in the final executable. Since Go 1.16, the go command supports including static files using the new //go:embed directive.

Examples

import _ "embed"

//go:embed hello.txt
var s string

import "embed"

//go:embed image/* template/*
var content embed.FS

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(content))))
tmpl := template.Must(template.ParseFS(content, "*.tmpl"))

Rules

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go standard library docs (pkg.go.dev), embed package; Go docs, Go 1.16 Release Notes. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). Copyright The Go Authors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go standard library docs (pkg.go.dev), embed package; Go docs, Go 1.16 Release Notes. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). Copyright The Go Authors.

Cite this

Permanent ID: iil:note:qymxhp7t

iinventedlight. 2026. "The //go:embed directive compiles files into a Go binary as a string, byte slice or embed.FS." iinventedlight Library, iil:note:qymxhp7t, version of 2026-11-18. https://iinventedlight.com/library/notes/go-embed/. License: CC-BY-4.0.

@misc{iil_note_qymxhp7t,
  author = {{iinventedlight}},
  title = {{The //go:embed directive compiles files into a Go binary as a string, byte slice or embed.FS}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/go-embed/},
  note = {Permanent ID iil:note:qymxhp7t. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-embed.md.