Step Over to Go Part 35 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

Linting and Formatting: gofmt Is Non-Negotiable

In C#, code formatting is a matter of preference. Tabs or spaces? Braces on the same line or next? Teams debate, .editorconfig files proliferate, and nobody quite agrees.

In Go, formatting is not a discussion. gofmt defines the One True Format, and everyone uses it. There is no debate because there's no choice.

This sounds authoritarian. It's actually liberating.

gofmt: The Formatter

Every Go file should be formatted by gofmt:

gofmt -w .              # format all files in place
go fmt ./...            # same thing, via go command

gofmt handles:

  • Indentation (tabs, not spaces)
  • Brace placement (same line, always)
  • Spacing around operators
  • Import grouping
  • Line length (it doesn't wrap, but long lines are a code smell)

There are no options. No configuration. One format for all Go code, everywhere.

The Philosophy

Rob Pike (Go co-creator): “Gofmt's style is no one's favourite, yet gofmt is everyone's favourite.”

The point isn't that gofmt's choices are optimal. The point is that having a choice wastes time. Every Go codebase looks the same, which means:

  • No bikeshedding about style
  • Pull reviews focus on logic, not formatting
  • New team members read unfamiliar code easily
  • Tooling (IDEs, linters) can assume standard formatting

goimports: gofmt Plus Import Management

goimports does everything gofmt does, plus:

  • Adds missing imports
  • Removes unused imports
  • Groups imports (stdlib, external, internal)
go install golang.org/x/tools/cmd/goimports@latest
goimports -w .

Most developers use goimports instead of plain gofmt. Configure your editor to run it on save.

Linting with golangci-lint

Formatting ensures consistency. Linting catches bugs and enforces best practices.

golangci-lint is the standard meta-linter. It runs many linters in one tool:

# Install
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

# Run
golangci-lint run

Default linters catch:

  • Unused variables and imports
  • Ineffective assignments
  • Missing error checks
  • Suspicious constructs

Common Linters

golangci-lint bundles dozens of linters. Key ones:

Linter Purpose
errcheck Unchecked errors
gosimple Simplification suggestions
govet Suspicious constructs
ineffassign Ineffectual assignments
staticcheck Advanced static analysis
unused Unused code
gosec Security issues
misspell Spelling mistakes
gocyclo Cyclomatic complexity
gocritic Opinionated style issues

Configuration

Update, November 2026: This config is the golangci-lint v1 format. v2 changed the shape and golangci-lint migrate converts it; the check-shadowing key was already gone. What I got wrong about Go has the details, checked against Go 1.27.

Create .golangci.yml:

linters:
  enable:
    - errcheck
    - gosimple
    - govet
    - ineffassign
    - staticcheck
    - unused
    - gosec
    - misspell
    
linters-settings:
  errcheck:
    check-type-assertions: true
  govet:
    check-shadowing: true
  gocyclo:
    min-complexity: 15

issues:
  exclude-rules:
    - path: _test\.go
      linters:
        - gosec  # less strict in tests

Run with:

golangci-lint run
golangci-lint run --fix  # auto-fix where possible

staticcheck

The most powerful individual linter. It catches subtle bugs:

// staticcheck catches this
if x == nil {
    return x.String()  // nil dereference
}

// And this
for i := range items {
    go func() {
        process(items[i])  // captures loop variable
    }()
}

Many issues that would be runtime errors become compile-time warnings.

Comparing to .NET Analyzers

Feature .NET Analyzers Go Linting
Format enforcement Optional (EditorConfig) Mandatory (gofmt)
Built-in Some analyzers gofmt, go vet
Meta-linter StyleCop, Roslyn golangci-lint
IDE integration Excellent Good
CI integration Easy Easy
Auto-fix Many rules Some rules

.NET has the advantage of the Roslyn compiler platform. Analyzers can be very sophisticated. Go's linters are simpler but catch most issues.

Editor Integration

VS Code

Install the Go extension. Add to settings.json:

{
    "go.formatTool": "goimports",
    "go.lintTool": "golangci-lint",
    "editor.formatOnSave": true,
    "[go]": {
        "editor.defaultFormatter": "golang.go",
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        }
    }
}

GoLand

Built-in support. Enable “Reformat code” and “Optimize imports” on save.

Vim/Neovim

Use vim-go or configure gopls (the Go language server) with your LSP setup.

CI Integration

Add to your pipeline:

# GitHub Actions
- name: golangci-lint
  uses: golangci/golangci-lint-action@v4
  with:
    version: latest

Or manually:

# Install
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin

# Run
golangci-lint run --timeout 5m

Pre-commit Hooks

Enforce formatting before commit:

# .git/hooks/pre-commit
#!/bin/sh
gofmt -l . | read && echo "Files not formatted" && exit 1
golangci-lint run && exit 0 || exit 1

Or use pre-commit:

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/golangci/golangci-lint
    rev: v1.55.0
    hooks:
      - id: golangci-lint

The Honest Take

Go's enforced formatting is one of its best features. No arguments, no decisions, no config files.

What Go does better:

  • No bikeshedding. Ever.
  • All code looks the same
  • gofmt is fast and universal
  • No configuration to maintain

What .NET does better:

  • More formatting options (if you want them)
  • Roslyn analyzers are very powerful
  • Better code action suggestions
  • More sophisticated refactoring

The verdict: You'll miss zero formatting decisions. You won't miss the arguments about formatting. golangci-lint catches most issues; enable it and move on.

The Go community's attitude is refreshing: format your code, lint it, and spend your energy on actual problems.


Next up: code generation with go generate. Why Go developers love generating code.