---
title: "Making code-generation in Go more powerful with generics"
description: "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."
url: "https://swe.quest/articles/making-code-generation-in-go-more-powerful-with-generics-5b72d91ebe8db40c"
canonical: "https://incident.io/blog/code-generation"
source: "incident.io"
published: "2022-10-20"
difficulty: "medium"
kind: "How-to"
reading_minutes: 10
tags: ["go","type-systems","testing","developer-productivity"]
---

# Making code-generation in Go more powerful with generics

A post on [incident.io](https://swe.quest/sources/incident-io.md), published 2022-10-20, 10 min read, How-to. Original: [https://incident.io/blog/code-generation](https://incident.io/blog/code-generation)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Go](https://swe.quest/tags/go.md) (Languages)
- [Type systems](https://swe.quest/tags/type-systems.md) (Compilers & runtimes)
- [Testing](https://swe.quest/tags/testing.md) (Engineering practices)
- [Developer productivity](https://swe.quest/tags/developer-productivity.md) (Engineering practices)

## Prerequisites

- Go generics ([easy posts on Go](https://swe.quest/tags/go.md?difficulty=easy))
- Go struct zero values ([easy posts on Go](https://swe.quest/tags/go.md?difficulty=easy))
- Gorm ORM basics
- Gomega testing ([easy posts on Testing](https://swe.quest/tags/testing.md?difficulty=easy))

## Keywords

Go generics, incident-io/partial, Gorm, Gomega, gstruct, samber/lo

## Related posts

- [How Render Enforces Access Controls with Go Generics](https://swe.quest/articles/how-render-enforces-access-controls-with-go-generics-6a60bf3827f53ed7.md): Render · 2024-04-16 · Medium · 6 min read · Go, Identity & auth, Type systems, Application security
  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.
- [DevEx matters for coding agents, too](https://swe.quest/articles/devex-matters-for-coding-agents-too-3cf5bcfa910d65e5.md): incident.io · 2025-12-19 · Medium · 11 min read · Developer productivity, TypeScript, Build systems, AI for coding, Developer tools
  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.
- [Engineering nits: Generating code faster](https://swe.quest/articles/engineering-nits-generating-code-faster-1820d3fbfc60303a.md): incident.io · 2023-12-11 · Medium · 5 min read · Go, Developer productivity, Performance engineering, Build systems
  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.
- [How to Write Code Without Having to Read It](https://swe.quest/articles/how-to-write-code-without-having-to-read-it-1700fbc56278687d.md): Shopify Engineering · 2022-10-11 · Medium · 12 min read · Type systems, Testing, Refactoring & migrations, TypeScript, Developer productivity
  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.
- [Ready, steady, goa: our API setup](https://swe.quest/articles/ready-steady-goa-our-api-setup-f9a8a900b508af76.md): incident.io · 2025-08-11 · Medium · 10 min read · API design, Go, TypeScript, Developer productivity
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
