HowtoGo
Home / Standard Library / The embed Package
Standard Library

The embed Package

embed compiles files into the binary. Templates, migrations, and static assets ship inside the executable, so deploying is one file with nothing beside it.

Examples

A string or []byte takes exactly one file. An embed.FS takes a whole tree and satisfies fs.FS, which is what makes it work with anything that reads a filesystem.

import "embed"

//go:embed version.txt
var version string

//go:embed logo.png
var logo []byte

//go:embed static
var assets embed.FS
Output
hello from embed
body { color: red }
FunctionDescription
//go:embed pattern
A directive, not a function. It must sit immediately above a package-level var, with no blank line between.
type FS struct
A read-only filesystem holding the embedded files. Satisfies fs.FS, fs.ReadDirFS, and fs.ReadFileFS.
(FS) Open(name string) (fs.File, error)
Opens one embedded file. Paths use forward slashes on every platform.
(FS) ReadFile(name string) ([]byte, error)
assets.ReadFile("static/site.css")
Reads a whole embedded file with no allocation beyond the returned slice.
(FS) ReadDir(name string) ([]fs.DirEntry, error)
Lists an embedded directory, sorted by filename.
string
//go:embed version.txt var version string
A var of type string takes the contents of exactly one file.
[]byte
A var of type []byte also takes exactly one file, unparsed.
all: prefix
Includes files starting with . or _, which the default patterns skip.
io/fs html/template net/http