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
- Put the directive immediately before a line declaring a single variable; only blank lines and
//line comments may sit between them. - Patterns use
path.Matchsyntax, relative to the package directory containing the source file, with a forward slash as separator even on Windows systems. - A pattern may not begin or end with a slash, and may not contain
.,..or empty path elements. - If a pattern names a directory, all files in the subtree are embedded recursively, except that files with names beginning with
.or_are excluded. A pattern that begins with the prefixall:changes the rule to include those files, for exampleall:image.
See also
Claims
- id: C1 claim: go:embed initializes a string, []byte or FS from package files. source: S1 evidence: "can use the //go:embed directive to initialize a variable of type string, []byte, or FS"
- id: C2 claim: Directory embeds exclude names beginning with . or _. source: S1 evidence: "all files in the subtree rooted at that directory are embedded (recursively)"
- id: C3 claim: Patterns are relative to the package directory. source: S1 evidence: "The patterns are interpreted relative to the package directory containing the source file."
- id: C4 claim: Go 1.16 added the //go:embed directive. source: S2 evidence: "using the new //go:embed directive"
Sources
- [S1] embed package. Go standard library docs (pkg.go.dev). https://pkg.go.dev/embed . License: BSD-3-Clause. Accessed 2026-11-18.
- [S2] Go 1.16 Release Notes. Go docs. https://go.dev/doc/go1.16 . License: CC-BY-4.0. Accessed 2026-11-18.
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
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.},
}
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.