Ridiculously simple Go build info 🛠️
go get go.followtheprocess.codes/build@latestpackage main
import (
"fmt"
"os"
"go.followtheprocess.codes/build"
)
func main() {
info, ok := build.Info()
if !ok {
fmt.Fprintf(os.Stderr, "could not get build info")
os.Exit(1)
}
fmt.Printf("%s\n", info)
}Gets you...
go: go1.23.2
path: go.followtheprocess.codes/build/cmd/build
os: darwin
arch: amd64
vcs: git
version: (devel)
commit: 5e8b8a68867eff5f754bfecdbc8baeb2c14c711c
dirty: true
time: 2024-10-06T10:39:12Z
main: mod go.followtheprocess.codes/build (devel) Tip
It's also JSON serialisable!
package main
import (
"encoding/json"
"fmt"
"os"
"go.followtheprocess.codes/build"
)
func main() {
info, _ := build.Info()
if err := json.NewEncoder(os.Stdout).Encode(info); err != nil {
fmt.Fprintf(os.Stderr, "could not write JSON: %v\n", err)
os.Exit(1)
}
}Gets you...
{
"main": {
"path": "go.followtheprocess.codes/build",
"version": "(devel)"
},
"time": "2024-10-06T10:39:12Z",
"go": "go1.23.2",
"path": "go.followtheprocess.codes/build/cmd/build",
"os": "darwin",
"arch": "amd64",
"vcs": "git",
"version": "(devel)",
"dirty": true
}build.Info returns a BuildInfo struct from which you can take any component of the build info:
package main
import (
"fmt"
"os"
"go.followtheprocess.codes/build"
)
func main() {
info, ok := build.Info()
if !ok {
fmt.Fprintf(os.Stderr, "could not get build info")
os.Exit(1)
}
fmt.Printf("Version: %s\n", info.Version)
fmt.Printf("Commit: %s\n", info.Commit)
}This package is wholly based on the Go internal implementation of runtime/debug.BuildInfo, this is just a slightly nicer wrapper that makes it easier to access common settings