Embed Static Files and Build Versions Into a Go Binary
Bundle static files with //go:embed and embed.FS, serve subdirectories cleanly with fs.Sub, and inject version strings with -ldflags -X or debug.ReadBuildInfo. After this, your Go binary ships assets and version info with zero external files.
Skip ahead
Already embedding single files with //go:embed? Jump to Case 2: serving embedded folders with embed.FS and fs.Sub or Case 3: stamping version metadata with -ldflags and debug.ReadBuildInfo.
Go compiles to a single executable—until your service tries to open templates/index.html or migrations/001.sql from disk and crashes with no such file or directory whenever someone runs the binary from a different working directory or forgets a COPY line in a Dockerfile. Meanwhile, binaries deployed without build metadata leave on-call engineers guessing which release commit is actually running.
Go solves both inside the standard toolchain: //go:embed compiles files and folders straight into the executable, and -ldflags="-X ..." + runtime/debug.ReadBuildInfo() stamp release tags and Git commits at build time. Verified on Go 1.27.
Case 1: Embed a file and inject a release version with -ldflags
Notice two small requirements: the blank import _ "embed" is required whenever you embed into a string or []byte, and -X only overwrites package-level var strings—never const:
package main
import (
_ "embed"
"fmt"
"runtime/debug"
)
//go:embed version_note.txt
var note string
var version = "dev"
func main() {
commit := "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
for _, s := range info.Settings {
if s.Key == "vcs.revision" {
commit = s.Value
}
}
}
fmt.Printf("version=%s commit=%s note=%q\n", version, commit, note)
}
$ go build -ldflags="-s -w -X main.version=v1.4.2" -o app .
$ ./app
version=v1.4.2 commit=unknown note="embedded ok"
When you run go build inside a Git repository, Go automatically stamps vcs.revision, vcs.time, and vcs.modified into debug.ReadBuildInfo() for free—even without passing -X!
Case 2: Embed an entire directory with embed.FS and fs.Sub
When shipping dozens of SQL migrations, HTML templates, or static web assets, embed the folder into an embed.FS (which satisfies fs.FS). Use fs.Sub to strip the leading folder prefix before passing it to http.FileServerFS:
//go:embed static/*
var rawAssets embed.FS
func assetHandler() (http.Handler, error) {
sub, err := fs.Sub(rawAssets, "static")
if err != nil {
return nil, err
}
return http.FileServerFS(sub), nil
}
Because embed.FS lives read-only inside the binary's data segment, concurrent HTTP handlers read files with zero disk I/O, zero locks, and zero container permission issues.
Case 3: Injecting -X into an internal package
In larger repositories where build metadata lives in internal/buildinfo/version.go instead of package main, pass the full module import path to -X:
$ go build -ldflags="-s -w -X example.com/ordersvc/internal/buildinfo.Version=v2.1.0" ./cmd/api
If -X ever fails silently to update a string, inspect the exact symbol name in your compiled binary with go tool nm ./app | grep Version.
Case 4: Including dotfiles with all:
By default, //go:embed static/* skips hidden dotfiles (such as .well-known/) and names starting with _. Add the all: prefix (//go:embed all:static) whenever dotfiles must be packaged.
💡 Tip
Expose a -version flag (and log version + vcs.revision once on startup via slog.Info) so container logs always prove which commit is live.
Common mistakes
- Using
const version = "dev"instead ofvar version = "dev": Constants are inlined by the compiler, so-Xsilently leaves them unchanged. - Using parent paths like
//go:embed ../sql: Forbidden by Go for security; place the embed declaration in the parent package and export theembed.FSvariable. - Embedding
.envfiles or TLS private keys: Embedded bytes sit unencrypted inside the binary and are trivially dumped withstrings ./app.
⚠ Warning
Do not embed gigabyte-sized media or ML weights with //go:embed—every embedded byte inflates executable size, slows CI uploads, and increases memory mapping overhead at startup. Keep //go:embed for templates, SQL migrations, and small web assets.
What happens if you skip this
Deployments fail with missing-file errors whenever working directories or Dockerfiles drift, and incident debugging stalls because nobody can confirm which release is running on a pod.
Related course: Go Advanced.
About
Bundle static files with //go:embed and embed.FS, serve subdirectories cleanly with fs.Sub, and inject version strings with -ldflags -X or debug.ReadBuildInfo. After this, your Go binary ships assets and version info with zero external files.
- Responsible
- Jay P
- Last Update
- 11/27/2026
- Reading Time
- 4 min
Reviews
Sign in to write a reviewComments
Sign in to commentNo comments yet.