go 24 lines · 1 tab

Expose build metadata for debugging deploys

Leah Thompson Jan 2026
1 tab
package api

import (
  "net/http"
  "runtime/debug"
)

var (
  Version = "dev"
  Commit  = "unknown"
)

func VersionHandler(w http.ResponseWriter, _ *http.Request) {
  info, _ := debug.ReadBuildInfo()
  w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  _, _ = w.Write([]byte("version=" + Version + "
"))
  _, _ = w.Write([]byte("commit=" + Commit + "
"))
  if info != nil {
    _, _ = w.Write([]byte("go=" + info.GoVersion + "
"))
  }
}
1 file · go Explain with highlit

When you’re on call, you eventually ask: “what version is running?” I expose a tiny /version endpoint that returns build metadata derived from debug.ReadBuildInfo plus a few variables set at build time. The goal isn’t perfect SBOMs; it’s fast debugging: you can confirm commit SHA, module versions, and Go version without SSHing into anything. I like to include this info in logs at startup too, but the endpoint is useful for automation and for health-check dashboards. The key is to keep it non-sensitive: don’t include secrets or internal URLs. In production, I usually guard it behind internal auth or only expose it on an admin listener. This is a low-effort, high-value observability feature.


Related snips

Share this code

Here's the card — post it anywhere.

Expose build metadata for debugging deploys — share card
Link copied