Skip to main content
Embedding your frontend into the Go binary creates a single executable with no external dependencies.

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

Single layer, minimal size.

4. Immutable Deployments

Frontend and backend versions always match.

Directory Structure

Before Embed

Embed Directive Location

The path is relative to the .go file containing the directive.

Why Use fs.Sub?

The embed directive includes the directory name:
Use fs.Sub to remove the prefix:
Now index.html is at the root, as expected.

Complete Example

Build Process

Binary Size

Embedding increases binary size:
Tips to reduce 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
Use file system when:
  • Frequent frontend updates
  • Want to update without rebuild
  • Very large frontends
  • Need separate frontend deployment

Hybrid Approach

Use environment variable to switch:

Multiple Embedded Directories

Embed views and static separately:

Next Steps

Building

Build process and optimization

Static Hosting

Alternative deployment options

Production

Production best practices