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

Generate All the Things

Go developers love code generation. Where C# uses reflection, attributes, and source generators, Go often uses tools that generate code before compilation.

This sounds backwards. Writing code that writes code? But it has advantages: generated code is inspectable, debuggable, and has zero runtime overhead.

The go generate Command

Add a magic comment to your source file:

//go:generate stringer -type=Status

type Status int

const (
    StatusPending Status = iota
    StatusActive
    StatusCompleted
)

Run:

go generate ./...

This runs stringer -type=Status in the package directory, which generates status_string.go:

// Code generated by "stringer -type=Status"; DO NOT EDIT.

func (i Status) String() string {
    switch i {
    case StatusPending:
        return "StatusPending"
    case StatusActive:
        return "StatusActive"
    case StatusCompleted:
        return "StatusCompleted"
    }
    return "Status(" + strconv.FormatInt(int64(i), 10) + ")"
}

The generated code is committed to your repo. It's just Go code.

Common Code Generators

stringer

Generates String() methods for constants:

//go:generate stringer -type=LogLevel

We covered this in the enums post.

mockgen

Generates mock implementations:

//go:generate mockgen -source=repository.go -destination=mock_repository.go -package=repo

Creates mock structs for testing.

sqlc

Generates type-safe database code from SQL:

-- queries/users.sql
-- name: GetUser :one
SELECT id, name, email FROM users WHERE id = $1;

-- name: ListUsers :many
SELECT id, name, email FROM users ORDER BY name;
//go:generate sqlc generate

Produces:

func (q *Queries) GetUser(ctx context.Context, id int64) (User, error) {
    row := q.db.QueryRowContext(ctx, getUserSQL, id)
    var i User
    err := row.Scan(&i.ID, &i.Name, &i.Email)
    return i, err
}

Type-safe queries with no reflection.

protoc (Protocol Buffers)

Generates Go code from .proto files:

//go:generate protoc --go_out=. --go-grpc_out=. api.proto

oapi-codegen

Generates code from OpenAPI specs:

//go:generate oapi-codegen -package api -generate types,server openapi.yaml

ent

Generates ORM code:

//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate ./schema

Writing Your Own Generator

Generators are just programs. You can write your own:

// gen/main.go
package main

import (
    "os"
    "text/template"
)

var tmpl = `// Code generated; DO NOT EDIT.
package {{.Package}}

var Version = "{{.Version}}"
var BuildTime = "{{.BuildTime}}"
`

func main() {
    t := template.Must(template.New("version").Parse(tmpl))
    
    data := map[string]string{
        "Package":   os.Getenv("GOPACKAGE"),
        "Version":   os.Getenv("VERSION"),
        "BuildTime": time.Now().Format(time.RFC3339),
    }
    
    f, _ := os.Create("version_gen.go")
    defer f.Close()
    t.Execute(f, data)
}

Use it:

//go:generate go run gen/main.go

The go:generate Directive

The comment format:

//go:generate command arg1 arg2

Rules:

  • Must start with //go:generate (no space after //)
  • The command runs in the package directory
  • Environment variables available: $GOPACKAGE, $GOFILE, $GOLINE
  • Output to stderr shows during generation

Multiple directives are fine:

//go:generate stringer -type=Status
//go:generate mockgen -source=service.go -destination=mock_service.go

Comparing to C# Source Generators

C# has source generators (Roslyn) that run during compilation:

[Generator]
public class MyGenerator : ISourceGenerator
{
    public void Execute(GeneratorExecutionContext context)
    {
        // Generate code based on syntax trees
    }
}
Aspect C# Source Generators Go go:generate
When runs During compilation Before compilation
Output In-memory Files in repo
Debugging Harder Easy (it's just files)
IDE support Integrated Manual regeneration
Dependencies Part of build External tools
Incremental Yes Manual

C#'s generators are more integrated. Go's are more transparent.

Best Practices

1. Commit generated code

Generated code should be in your repo:

  • CI doesn't need generator tools
  • Code review includes generated changes
  • git blame works

2. Mark generated files

Start with the standard header:

// Code generated by TOOL; DO NOT EDIT.

Linters and editors recognize this and treat the file specially.

3. Regenerate in CI

Verify generated code is up-to-date:

- name: Check generated code
  run: |
    go generate ./...
    git diff --exit-code

Fails if generated code differs from committed code.

4. Document generation

In your README or Makefile:

generate:
	go generate ./...

.PHONY: generate

5. Keep generators fast

go generate runs serially. Slow generators hurt iteration speed.

Real-World Example

A typical project might have:

// internal/domain/status.go
//go:generate stringer -type=Status

// internal/repository/repository.go  
//go:generate mockgen -source=repository.go -destination=mock_repository.go -package=repository

// internal/database/queries.sql
//go:generate sqlc generate

// api/openapi.yaml
//go:generate oapi-codegen -package api -generate types,server,spec -o api_gen.go openapi.yaml

Run go generate ./... and all these tools produce code.

The Honest Take

Code generation is more prominent in Go than C#. It's a cultural thing. Go prefers explicit, inspectable code over runtime magic.

What Go does well:

  • Generated code is transparent
  • Easy to debug (it's just files)
  • No runtime reflection overhead
  • Tools are standalone programs

What C# does better:

  • Source generators are integrated
  • IDE understands generated code immediately
  • Incremental generation
  • No manual regeneration step

The verdict: If you're not used to code generation, it feels weird. But once you embrace it, you appreciate the transparency.

sqlc in particular is wonderful: type-safe database access without ORM magic, because the queries are analyzed at generation time.

Learn to love go generate ./.... It's part of the Go workflow.


Next up: debugging with Delve. Getting productive with dlv after Visual Studio's polished debugging experience.