Read embed and build metadata
Read embed and build metadata
embed incorporates files at build time, while runtime/debug.ReadBuildInfo reports available metadata about the running Go binary. They solve different problems: shipping a static asset and inspecting how a program was built. Metadata can be incomplete and is not a signature.
package main
import ( "embed" "fmt")
//go:embed main.govar source embed.FS
func main() { b, e := source.ReadFile("main.go") fmt.Println(e == nil, len(b) > 0) _, e = source.ReadFile("missing.txt") fmt.Println(e != nil)}Save this example as main.go in its own directory. Embedding the source itself avoids needing an extra asset; for an actual app, embed a reviewed static asset such as templates/*.html instead. Patterns are relative to the package directory; embed.FS is read-only. Files are fixed at build time. Never embed credentials.
Expected output:
true truetrueInspect build information
package main
import ( "fmt" "runtime" "runtime/debug" "strings")
func main() { info, ok := debug.ReadBuildInfo() fmt.Println(ok) if ok { fmt.Println(strings.HasPrefix(info.GoVersion, "go")) fmt.Println(info.GoVersion == runtime.Version()) }}ReadBuildInfo returns an availability boolean; inspect it first. Module versions, replacements and build settings may be present depending on build method. debug/buildinfo.ReadFile inspects a trusted Go binary without running it. Do not assume version control fields exist. Record verified artifact hashes and provenance separately.
Expected output:
truetruetrueTry it locally
Read metadata from your own executable with debug/buildinfo.ReadFile and verify that it agrees with runtime.Version().
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Specialized library 06: Read embed and build metadata
Check the lesson objective before moving on.
When are embed bytes incorporated?
Does ReadBuildInfo prove artifact authenticity?