Explainers

Go embed: Prod Binary, Dev Live Files

Published
Duration
Language
English

Watch on YouTube ↗

Summary

Go's embed package makes production easy — one binary with assets inside. Development hurts when every CSS or template change needs a rebuild. This explainer shows the fs.FS pattern: embed.FS in prod, os.DirFS in dev, same FileServer and ParseFS, via build tags or a runtime switch. Verified with Go 1.27.3.

Transcript

Text of the video's narration.

embed is easy in prod. Dev is where it hurts. Let me show you. In production, go colon embed copies your static files into the binary at build time. Delete the static folder on the server and the site still serves. One file to ship.

In development, that copy is frozen. Every CSS tweak means another build. Your gopher starts drinking coffee professionally. The fix is not more magic. Program against io/fs.FS, not embed.FS itself. One file, tagged not dev, embeds the tree and trims it with fs.Sub.

Another file, tagged dev, returns os.DirFS. Same function. Same FileServer. Same ParseFS. Run with dash tags dev, edit, refresh: the file server reads from disk. Templates too, if you parse them per request in dev. Or flip it at runtime, Brandur style: production uses the embed, everything else reads disk.

One gotcha: DirFS paths follow your working directory, not your package. Speak fs.FS. Embed for the ship. Disk for the loop.

Sources

Credits and notes

  • Visuals and narration generated with AI tools.

More in this series · Explainers