From 03e49e73a065f0a2df1fd3ac3392754e935b2aff Mon Sep 17 00:00:00 2001 From: sneak Date: Thu, 1 Oct 2026 22:50:34 +0000 Subject: [PATCH] Read the architecture at run time, not via a Buildarch ldflag (closes #66) 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 --- prompts/CODE_STYLEGUIDE_GO.md | 13 +++++------ prompts/GO_HTTP_SERVER_CONVENTIONS.md | 32 ++++++++++----------------- 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/prompts/CODE_STYLEGUIDE_GO.md b/prompts/CODE_STYLEGUIDE_GO.md index 2869855..d91a35c 100644 --- a/prompts/CODE_STYLEGUIDE_GO.md +++ b/prompts/CODE_STYLEGUIDE_GO.md @@ -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 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 - 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: @@ -35,16 +36,14 @@ last_modified: 2026-09-08 import ( "fmt" + "runtime" ) - var ( - Version string - Buildarch string - ) + var Version string func main() { 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, # `ARG VERSION` puts it in the environment and `?=` defers to it. VERSION ?= $(shell git describe --always --dirty) - BUILDARCH := $(shell uname -m) GOLDFLAGS += -X main.Version=$(VERSION) - GOLDFLAGS += -X main.Buildarch=$(BUILDARCH) # osx can't statically link apparently?! ifeq ($(UNAME_S),Darwin) diff --git a/prompts/GO_HTTP_SERVER_CONVENTIONS.md b/prompts/GO_HTTP_SERVER_CONVENTIONS.md index 3ee41d2..72d528c 100644 --- a/prompts/GO_HTTP_SERVER_CONVENTIONS.md +++ b/prompts/GO_HTTP_SERVER_CONVENTIONS.md @@ -118,15 +118,13 @@ import ( ) var ( - Appname string = "CHANGEME" - Version string - Buildarch string + Appname string = "CHANGEME" + Version string ) func main() { globals.Appname = Appname globals.Version = Version - globals.Buildarch = Buildarch fx.New( fx.Provide( @@ -826,7 +824,7 @@ func (l *Logger) Identify() { l.log.Info("starting", "appname", l.params.Globals.Appname, "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) var ( - Appname string - Version string - Buildarch string + Appname string + Version string ) // Struct for DI type Globals struct { - Appname string - Version string - Buildarch string + Appname string + Version string } func New(lc fx.Lifecycle) (*Globals, error) { n := &Globals{ - Appname: Appname, - Buildarch: Buildarch, - Version: Version, + Appname: Appname, + Version: Version, } return n, nil } @@ -973,15 +968,13 @@ func New(lc fx.Lifecycle) (*Globals, error) { ```go // cmd/httpd/main.go var ( - Appname string = "CHANGEME" // Default, overridden by build - Version string // Set at build time - Buildarch string // Set at build time + Appname string = "CHANGEME" // Default, overridden by build + Version string // Set at build time ) func main() { globals.Appname = Appname 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 # to it. VERSION ?= $(shell git describe --tags --always) -BUILDARCH := $(shell go env GOARCH) build: - go build -ldflags "-X main.Version=$(VERSION) -X main.Buildarch=$(BUILDARCH)" ./cmd/httpd + go build -ldflags "-X main.Version=$(VERSION)" ./cmd/httpd ``` --- -- 2.54.0