go:embed
Use `//go:embed` for small static assets shipped with the binary; keep patterns explicit and package-local.
Canonical guidance
- use
//go:embedfor static assets needed at runtime - keep patterns explicit and package-local
- choose
embed.FSwhen a file tree orfs.FSintegration matters
Use when
- templates
- small static web assets
- fixture data that should travel with the binary
Avoid
- embedding huge mutable data sets
- broad patterns that pull in unintended files
- confusing embed with runtime configuration
Preferred pattern
import "embed"
//go:embed templates/*.html
var templates embed.FS
Anti-pattern
- embedding development-only assets because it was simpler than fixing the runtime file lookup path
Explanation: This anti-pattern is tempting because it removes one deployment problem, but it quietly bloats the binary and hides asset ownership.
Why
- embedding is excellent for stable static assets, not for everything that happens to live on disk
Related pages
Sources
- embed package - Go Team