Read the architecture at run time, not via a Buildarch ldflag (closes #66)
check / check (push) Successful in 25s

The Go styleguide and the HTTP server conventions no longer pass the
build architecture in through the Makefile. The Buildarch variable,
globals field and BUILDARCH Makefile lines are removed from every
example; the styleguide example prints runtime.GOARCH, and the
logger's Identify logs "arch", runtime.GOARCH. The styleguide item
gains one sentence saying so.

Model: opus-5-5
This commit was merged in pull request #67.
This commit is contained in:
2026-10-02 01:03:38 +02:00
parent ed5ed236b1
commit 2ae9391b26
2 changed files with 17 additions and 28 deletions
+5 -8
View File
@@ -24,7 +24,8 @@ last_modified: 2026-09-08
1. Embed the git commit hash into the binary and include it in startup logs and 1. Embed the git commit hash into the binary and include it in startup logs and
in health check output. This is to make it easier to correlate running in health check output. This is to make it easier to correlate running
instances with their code. Do not include build time or build user, as these instances with their code. Do not include build time or build user, as these
will make the build nondeterministic. will make the build nondeterministic. The architecture is not passed in at
build time; a program that reports it reads `runtime.GOARCH` at run time.
Example relevant Makefile sections: Example relevant Makefile sections:
@@ -35,16 +36,14 @@ last_modified: 2026-09-08
import ( import (
"fmt" "fmt"
"runtime"
) )
var ( var Version string
Version string
Buildarch string
)
func main() { func main() {
fmt.Printf("Version: %s\n", Version) fmt.Printf("Version: %s\n", Version)
fmt.Printf("Buildarch: %s\n", Buildarch) fmt.Printf("Arch: %s\n", runtime.GOARCH)
} }
``` ```
@@ -57,10 +56,8 @@ last_modified: 2026-09-08
# `--build-arg VERSION=...`; where a build stage invokes make, # `--build-arg VERSION=...`; where a build stage invokes make,
# `ARG VERSION` puts it in the environment and `?=` defers to it. # `ARG VERSION` puts it in the environment and `?=` defers to it.
VERSION ?= $(shell git describe --always --dirty) VERSION ?= $(shell git describe --always --dirty)
BUILDARCH := $(shell uname -m)
GOLDFLAGS += -X main.Version=$(VERSION) GOLDFLAGS += -X main.Version=$(VERSION)
GOLDFLAGS += -X main.Buildarch=$(BUILDARCH)
# osx can't statically link apparently?! # osx can't statically link apparently?!
ifeq ($(UNAME_S),Darwin) ifeq ($(UNAME_S),Darwin)
+12 -20
View File
@@ -118,15 +118,13 @@ import (
) )
var ( var (
Appname string = "CHANGEME" Appname string = "CHANGEME"
Version string Version string
Buildarch string
) )
func main() { func main() {
globals.Appname = Appname globals.Appname = Appname
globals.Version = Version globals.Version = Version
globals.Buildarch = Buildarch
fx.New( fx.New(
fx.Provide( fx.Provide(
@@ -826,7 +824,7 @@ func (l *Logger) Identify() {
l.log.Info("starting", l.log.Info("starting",
"appname", l.params.Globals.Appname, "appname", l.params.Globals.Appname,
"version", l.params.Globals.Version, "version", l.params.Globals.Version,
"buildarch", l.params.Globals.Buildarch, "arch", runtime.GOARCH,
) )
} }
``` ```
@@ -946,23 +944,20 @@ import "go.uber.org/fx"
// Package-level variables (set from main) // Package-level variables (set from main)
var ( var (
Appname string Appname string
Version string Version string
Buildarch string
) )
// Struct for DI // Struct for DI
type Globals struct { type Globals struct {
Appname string Appname string
Version string Version string
Buildarch string
} }
func New(lc fx.Lifecycle) (*Globals, error) { func New(lc fx.Lifecycle) (*Globals, error) {
n := &Globals{ n := &Globals{
Appname: Appname, Appname: Appname,
Buildarch: Buildarch, Version: Version,
Version: Version,
} }
return n, nil return n, nil
} }
@@ -973,15 +968,13 @@ func New(lc fx.Lifecycle) (*Globals, error) {
```go ```go
// cmd/httpd/main.go // cmd/httpd/main.go
var ( var (
Appname string = "CHANGEME" // Default, overridden by build Appname string = "CHANGEME" // Default, overridden by build
Version string // Set at build time Version string // Set at build time
Buildarch string // Set at build time
) )
func main() { func main() {
globals.Appname = Appname globals.Appname = Appname
globals.Version = Version globals.Version = Version
globals.Buildarch = Buildarch
// ... // ...
} }
``` ```
@@ -999,10 +992,9 @@ Use ldflags to inject version information at build time:
# stage invokes make, `ARG VERSION` puts it in the environment and `?=` defers # stage invokes make, `ARG VERSION` puts it in the environment and `?=` defers
# to it. # to it.
VERSION ?= $(shell git describe --tags --always) VERSION ?= $(shell git describe --tags --always)
BUILDARCH := $(shell go env GOARCH)
build: build:
go build -ldflags "-X main.Version=$(VERSION) -X main.Buildarch=$(BUILDARCH)" ./cmd/httpd go build -ldflags "-X main.Version=$(VERSION)" ./cmd/httpd
``` ```
--- ---