How It Works
The//go:embed directive bundles files into the compiled binary:
Benefits
1. Single Binary Deployment
One file contains everything:2. No External Dependencies
3. Simpler Deployment
4. Immutable Deployments
Frontend and backend versions always match.Directory Structure
Before Embed
Embed Directive Location
.go file containing the directive.
Why Use fs.Sub?
The embed directive includes the directory name:fs.Sub to remove the prefix:
index.html is at the root, as expected.
Complete Example
Build Process
Binary Size
Embedding increases binary size:- Minify frontend
- Compress assets
- Remove unused code
- Use code splitting
Trade-offs
Pros
- ✅ Single file deployment
- ✅ No file serving issues
- ✅ Faster startup (no disk I/O)
- ✅ Simpler Docker images
- ✅ Version consistency
Cons
- ❌ Larger binary size
- ❌ Must rebuild for frontend changes
- ❌ Can’t update frontend independently
- ❌ Slower build times
When to Use
Use embedded FS when:- Deploying to production
- Want simple deployment
- Frontend changes infrequently
- Binary size acceptable
- Frequent frontend updates
- Want to update without rebuild
- Very large frontends
- Need separate frontend deployment