---
source: https://iinventedlight.com/library/notes/go-embed.md
title: "The //go:embed directive compiles files into a Go binary as a string, []byte or embed.FS"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Go standard library docs (pkg.go.dev)); CC-BY-4.0 (Go docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: Go embed"
tags: [go, embed, go-embed, embed.FS, static-files]
client: Librarian
sources:
  - id: S1
    title: "embed package"
    publisher: Go standard library docs (pkg.go.dev)
    url: https://pkg.go.dev/embed
    accessed: 2026-11-18
    license: "BSD-3-Clause"
  - id: S2
    title: "Go 1.16 Release Notes"
    publisher: Go docs
    url: https://go.dev/doc/go1.16
    accessed: 2026-11-18
    license: "CC-BY-4.0"
---
# 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

```go
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.Match` syntax, 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 prefix `all:` changes the rule to include those files, for example `all:image`.

## See also

- [go-modules-basics](https://iinventedlight.com/library/notes/go-modules-basics/)
- [go-testing](https://iinventedlight.com/library/notes/go-testing/)
- [go-structured-logging-slog](https://iinventedlight.com/library/notes/go-structured-logging-slog/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
