Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

incident.io 10 min read How-to mediumAdept

Making code-generation in Go more powerful with generics

Combining Go generics with code generation gives type-safe partial structs: a generic Partial[T] tracks which fields are set alongside a subject value, enabling safe partial updates through a Gorm wrapper that avoids zero-value overwrites and race conditions. Generated per-type builders (UserBuilder.Name(...)) remove typo-prone field-name strings and let the compiler reject setters from the wrong base type. Generated matchers wrap Gomega's gstruct so test assertions are type-checked, which catches field type changes such as null.String to string at compile time. The design principle is to put complex logic in readable generic code and generate only simple boilerplate. The library is open-sourced as incident-io/partial.

Related posts

  • How Render Enforces Access Controls with Go Generics

    Render mediumAdept

    Render enforces role-based access control at compile time by wrapping domain objects like Project in opaque types (AuthorizedProject) that can only be constructed through a permission-checking function. This guarantees model-layer code never runs without an authorization check. Go generics go further: each permission is a distinct type parameter, so passing an object authorized for "update" into a "delete" method is a type error, though tooling gaps around generics have kept that final step from being merged. Rare bootstrapping cases use an explicitly named UnsafeAuthorize() that gets extra review. The tradeoff is more code per new permission, but the compiler yields a complete list of codepaths to update, which caught real security holes before launch.

  • DevEx matters for coding agents, too

    incident.io mediumAdept

    incident.io cut its typical lint-plus-compile feedback loop from over 90 seconds to under 10, arguing that fast toolchains matter even more when AI coding agents generate code faster than slow tools can validate it. The gains came from replacing ESLint and Prettier with Biome (40s to under 1s), adopting the preview Go-based TypeScript compiler tsgo while keeping tsc on CI as a safety net, and switching tasks from Node/Yarn to Bun. The team also replaced the Java OpenAPI Generator with a ~700-line Bun/TypeScript generator, built with Claude Code to match the old output exactly, which cut generation from 45s to 0.21s. An upstream Goa PR added parallel file writing and cut its generation time from 52s to 10s.

  • Engineering nits: Generating code faster

    incident.io mediumAdept

    incident.io cut a Go code-generation step from about 16 seconds to under half a second (97% faster) by adding commonly used imports directly to the generator's template. The bottleneck was goimports resolving missing imports, which requires scanning every package in the module and its dependencies, repeated hundreds of times. Removing unused imports is cheap because it only inspects a single file, so over-including imports (with the list built via `go list`) leaves goimports little to resolve. The roughly 20-line change saves about 3 engineer-hours per week for a team of around 20.

  • How to Write Code Without Having to Read It

    Shopify Engineering mediumAdept

    Engineers can safely change unfamiliar code with minimal reading by letting the compiler, linter and unit tests guide them: locate the entrypoint via user-visible strings (or git bisect for regressions), make the breaking change at the leaf component, then climb the callstack following build errors and sweep back down via lint errors, exhaustive switches and test failures. To make code easier for the next "non-reader," replace comments with enforced checks, wrap primitives in custom types, merge dependent arguments into tagged unions, nested objects or enums, and split decision logic into pure functions so unit tests stay simple. Examples use React Native and TypeScript but the approach generalizes to any typed, tested codebase.

  • Ready, steady, goa: our API setup

    incident.io mediumAdept

    incident.io defines its HTTP API in Goa's design-first DSL, which generates net/http transport handlers, validation code, typed Go service interfaces, OpenAPI 3.0 specs and Go clients. This replaces hand-written Gin handlers with manual parsing, auth and serialization. On top of Goa, a `make generate-api` pipeline pretty-prints and merges the OpenAPI output and filters it into internal, mobile and public variants. Those variants feed specialised clients: typed TypeScript for the React frontend, lean mobile clients and Go test clients. A single DSL change then flows through to server code, docs and compile-time-checked frontend types.

The quest for software engineering knowledge.

Look:
Layout: