//go:embed directive is a compiler directive that allows programs to include arbitrary files and folders in the Go binary at build time. This is useful for bundling static assets, configuration files, templates, and other resources directly into your executable.
What is go:embed?
//go:embed embeds the contents of files into variables at compile time, making them part of your binary. The files become available without needing to distribute them separately.Benefits
- Single binary deployment (no external file dependencies)
- Guaranteed file availability at runtime
- Simplified distribution and deployment
- Version-locked assets (files can’t change after build)
Basic Usage
First, import theembed package:
If you don’t use any exported identifiers from the embed package, you can do a blank import:
import _ "embed"Embedding into String
Embed a file’s contents into a string variable:Embedding into Byte Slice
Embed a file’s contents into a byte slice:Embedding Multiple Files
Useembed.FS to embed multiple files or folders:
embed.FS implements a simple virtual file system that provides methods like ReadFile, ReadDir, and Open.Path Rules
Relative Paths
Wildcards
Complete Example
Working with embed.FS
Reading Files
Opening Files
Reading Directories
Common Use Cases
- Web Templates
- Static Web Server
- Configuration
- SQL Migrations
Embedding Patterns
Single File
Embed individual files as strings or byte slices
Directory
Embed entire directories with wildcards
Multiple Patterns
Combine multiple embed directives for one variable
Filtered Content
Use wildcards to embed only specific file types
Restrictions and Limitations
Hidden files excluded by default
Hidden files excluded by default
Only valid for package-level variables
Only valid for package-level variables
//go:embed can only be used with package-level variables, not local variables inside functions.Must be used with specific types
Must be used with specific types
Only valid with types:
string, []byte, or embed.FS.Paths must exist at build time
Paths must exist at build time
All embedded paths must exist when the code is built. Missing files cause build errors.
No symbolic links
No symbolic links
Symbolic links are not followed. The embedded filesystem contains the actual file contents.
Binary size considerations
Binary size considerations
Embedded files increase binary size. Consider whether to embed large files or load them at runtime.
Best Practices
Use embed.FS for multiple files
Use embed.FS for multiple files
When embedding more than one file, use
embed.FS instead of multiple string/byte variables.Organize embedded assets
Organize embedded assets
Keep embedded files in dedicated directories like
assets/, static/, or templates/.Handle errors properly
Handle errors properly
Always handle errors when reading from
embed.FS, even though files are guaranteed to exist:Document embedded files
Document embedded files
Add comments explaining what files are embedded and why:
Consider build tags for different environments
Consider build tags for different environments
HTTP File Server Example
Reading More
For more details about the embed directive, see the official documentation:
Related Topics
Reading Files
Learn about reading files at runtime
File Paths
Working with file paths in Go