What are the gotchas of go:embed patterns? Why is my .env or _redirects file missing?
Question 434HardGo 1.22 to 1.25
When a pattern names a directory, files whose names start with . or _ are excluded recursively. So //go:embed dist silently drops dist/.well-known/, _app/ (SvelteKit output) and _redirects. Fixes:
//go:embed all:dist // include dot/underscore files (Go 1.18+)
var dist embed.FS
//go:embed dist/_app dist/.well-known // or name them explicitly
var more embed.FS
Other rules:
- No
.., no absolute paths, and nothing outside the module. A matched path cannot cross into another module (a directory with its own go.mod) or intovendor/. - Symlinks and irregular files are not supported.
- A pattern that matches nothing, or an empty directory, is a compile error, which is useful.
*does not match/, and there is no**. - Paths inside the FS always use forward slashes and have no leading
./. With//go:embed staticyou open"static/index.html", not"index.html", so usefs.Sub. - Embed only works on package-level vars, not locals.
- Changes to embedded files correctly invalidate the build cache, but for local development you may want
os.DirFSbehind a flag to get hot reload. - Embedded files have a zero ModTime, so
http.FileServersends no Last-Modified header. Use ETags or hashed filenames for caching.
More on Modules, Packages & Tooling
- Q432How do staticcheck and golangci-lint differ from go vet? How would you set up linting for a team?
- Q433How does //go:embed work? Show the three target types.
- Q435What does this print? Explain Go's package initialization order.
- Q436Go forbids import cycles. How do you detect and break them?
- Q437A vulnerable dependency shows up in your build. How do you find out why it's there and upgrade it safely?
- Q438What is Profile-Guided Optimization (PGO) in Go and how do you use it in production?