Step Over to Go Part 37 of 50
  1. 1 Goodbye .csproj, Hello go.mod
  2. 2 The Phrasebook: C# Concepts in Go Terms
  3. 3 You Know .NET, Now Meet the Go Toolchain
  4. 4 No Enums? No Problem (Sort Of)
  5. 5 Generics: Late to the Party
  6. 6 The Million Dollar Mistake, Differently
  7. 7 Your Class Is Now a Struct (And That's Fine)
  8. 8 Where Did My Properties Go?
  9. 9 if err != nil: Your New Reality
  10. 10 Panic and Recover: The Emergency Exit
  11. 11 The switch Statement You Always Wanted
  12. 12 Stack vs Heap: Go's Escape Analysis
  13. 13 Pointers Without the Pain
  14. 14 Slices Are Not Arrays (And Neither Is List<T>)
  15. 15 Channels: The sync You Didn't Know You Wanted
  16. 16 Context Is King
  17. 17 Async/Await vs Goroutines: A Mindset Shift
  18. 18 select: Multiplexing Like a Pro
  19. 19 Mutexes and WaitGroups: When Channels Aren't the Answer
  20. 20 The Empty Interface and Type Assertions
  21. 21 Composition Over Inheritance (For Real This Time)
  22. 22 Implicit Interfaces: Scary Then Brilliant
  23. 23 Benchmarks and Profiling Out of the Box
  24. 24 Mocking in a Language Without Mockito
  25. 25 Multi-Module Repos: Monorepo Thinking
  26. 26 Organising a Real Project
  27. 27 Testing Without a Framework
  28. 28 Configuration Without IOptions<T>
  29. 29 Database Access: database/sql vs Entity Framework
  30. 30 Debugging: Delve vs Visual Studio's Comfort
  31. 31 Generate All the Things
  32. 32 GORM and Friends: When You Do Want an ORM
  33. 33 HTTP Services: net/http vs ASP.NET Core
  34. 34 JSON: Struct Tags and the Marshal Dance
  35. 35 Linting and Formatting: gofmt Is Non-Negotiable
  36. 36 Logging: slog and the Structured Logging Story
  37. 37 Shrinking Binaries and Build Tags
  38. 38 Dockerfile for Go: Simpler Than You'd Think
  39. 39 GitHub Actions for Go Projects
  40. 40 Health Checks and Readiness Probes
  41. 41 Kubernetes and Go: A Natural Fit
  42. 42 AWS Lambda and Go: Cold Starts That Don't Hurt
  43. 43 Metrics, Traces, and Logs: The OpenTelemetry Way
  44. 44 Single Binary Deploys: The Killer Feature
  45. 45 Versioning Your Modules
  46. 46 What I Actually Miss From C#
  47. 47 What I'll Never Go Back For
  48. 48 Six Months In: Was It Worth It?
  49. 49 Forty-Eight Posts on Moving From C# to Go, in the Right Order
  50. 50 Range-Over-Func Iterators for People Who Miss LINQ
About this series

Shrinking Binaries and Build Tags

Go binaries are already small compared to .NET self-contained deployments. But sometimes you want smaller: Lambda deployments, embedded systems, or just because.

Let's look at ldflags for stripping binaries and build tags for conditional compilation.

Stripping with ldflags

The simplest optimisation:

go build -ldflags="-s -w" -o myapp
Flag Effect Size Impact
-s Strip symbol table ~15-20% smaller
-w Strip DWARF debug info ~10-15% smaller

Combined, you might see 25-30% reduction.

Before:

go build -o myapp && ls -lh myapp
# 14M myapp

After:

go build -ldflags="-s -w" -o myapp && ls -lh myapp
# 10M myapp

Embedding Version Information

Use ldflags to set variables at build time:

// main.go
package main

var (
    version   = "dev"
    commit    = "none"
    buildTime = "unknown"
)

func main() {
    fmt.Printf("Version: %s, Commit: %s, Built: %s\n", version, commit, buildTime)
}

Build with values:

go build -ldflags="-s -w \
  -X main.version=1.2.3 \
  -X main.commit=$(git rev-parse --short HEAD) \
  -X main.buildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
  -o myapp

Now myapp --version shows real build info, baked in at compile time.

Build Tags: Conditional Compilation

Build tags let you include/exclude files based on conditions. It's Go's equivalent to #if DEBUG or conditional compilation symbols.

File-Level Tags

Add a comment at the top of a file:

//go:build linux

package main

// This file only compiles on Linux
func platformSpecificSetup() {
    // Linux-specific code
}

Or exclude a platform:

//go:build !windows

package main

// This file compiles everywhere except Windows

Combining Tags

//go:build linux && amd64

// Only Linux on x86-64
//go:build linux || darwin

// Linux or macOS
//go:build !cgo

// Only when CGO is disabled

Custom Build Tags

Define your own:

//go:build premium

package features

func PremiumFeatures() {
    // Only in premium builds
}

Build with:

go build -tags premium -o myapp-premium

Without the tag, files marked //go:build premium are excluded.

Common Use Cases

Debug vs Production:

//go:build debug

package main

func init() {
    log.SetLevel(log.DebugLevel)
}
//go:build !debug

package main

func init() {
    log.SetLevel(log.InfoLevel)
}

Feature Flags:

//go:build enterprise

package auth

func SSOLogin() { ... }

Platform-Specific Implementations:

mypackage/
├── file.go           // shared code
├── file_linux.go     // Linux-specific
├── file_windows.go   // Windows-specific
└── file_darwin.go    // macOS-specific

Go automatically selects by filename suffix: _linux.go, _windows.go, _darwin.go, _amd64.go, _arm64.go.

UPX Compression (Controversial)

UPX compresses executables:

go build -ldflags="-s -w" -o myapp
upx --best myapp

Before UPX: 10MB After UPX: 3MB

Sounds great, but:

  • Slower startup (decompression)
  • Some virus scanners flag UPX-packed binaries
  • Can interfere with debugging
  • Memory usage increases at runtime

Most Go developers skip UPX. The uncompressed binary is usually small enough.

Trimpath for Reproducibility

Remove local paths from the binary:

go build -trimpath -o myapp

Without -trimpath, stack traces contain your local paths:

/Users/alice/projects/myapp/main.go:42

With -trimpath:

myapp/main.go:42

Cleaner error messages and reproducible builds.

Comparing to .NET

Feature .NET Go
Strip debug info PublishTrimmed -ldflags="-s -w"
Conditional compilation #if DEBUG Build tags
Platform-specific Runtime checks or #if File suffixes
Build-time variables MSBuild properties -X main.var=value
IL trimming PublishTrimmed N/A (no IL)

.NET's trimming is more complex because it's trimming IL and dependencies. Go doesn't have this problem. It compiles to native code and includes only what's used.

A Complete Production Build

#!/bin/bash

VERSION=$(git describe --tags --always --dirty)
COMMIT=$(git rev-parse --short HEAD)
BUILD_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)

go build \
  -trimpath \
  -ldflags="-s -w \
    -X main.version=$VERSION \
    -X main.commit=$COMMIT \
    -X main.buildTime=$BUILD_TIME" \
  -o myapp \
  ./cmd/server

This produces:

  • Stripped binary (smaller)
  • No local paths (reproducible)
  • Version info embedded (debuggable in production)

Build Tags in Practice

A common pattern for different build configurations:

cmd/server/
├── main.go
├── config_dev.go      //go:build dev
├── config_prod.go     //go:build !dev

config_dev.go:

//go:build dev

package main

var defaultConfig = Config{
    LogLevel: "debug",
    Database: "localhost:5432",
}

config_prod.go:

//go:build !dev

package main

var defaultConfig = Config{
    LogLevel: "info",
    Database: "",  // must be set via env
}

Build:

go build -tags dev -o myapp-dev   # includes config_dev.go
go build -o myapp                  # includes config_prod.go

The Honest Take

Go's build customisation is simpler than .NET's MSBuild system.

What Go does well:

  • Simple ldflags for common needs
  • Build tags are straightforward
  • File suffixes for platform code
  • Fast rebuilds

What .NET does better:

  • More sophisticated trimming
  • Rich MSBuild conditionals
  • Better IDE support for conditional code
  • Source generators for build-time code

The verdict: For most cases, -ldflags="-s -w" and maybe a build tag or two is all you need. Go's simplicity means less configuration and fewer surprises.

If you're coming from complex MSBuild configurations, you might miss the flexibility. But you probably won't miss the complexity.


Next up: Dockerfiles for Go. Multi-stage builds, scratch images, and why Go containers are so small.