Skip to main content

One post tagged with "Design"

Why ant works the way it does, and the trade-offs.

View All Tags

Field pointers as column names: compile-checked Go queries without codegen

· 14 min read
Alberts Liepiņš
Maintainer of ant

Every time I've picked a database library for a Go project, the choice came down to which problem I'd rather live with. GORM and Bun take column names as strings, so Where("agee > ?", 18) compiles and fails when the query runs. ent and sqlc catch that at build time, but only after you've described your schema in their DSL or in SQL files and run a generator.

On a few projects I skipped both and just used stored procedures. (I'll touch on stored procedures later)

Go 1.27 added generic methods, and they make a third option practical: name columns with pointers to your own struct fields. In ant.Gt(&Users.Age, 18) the compiler checks that Age exists and that 18 is an int without needing any generated code. I built ant around that idea.

This post covers the technical approach on how a pointer turns back into a column, what the new generics allow us to do, what it costs, and what mistakes can't be caught by the compiler (yet)

The code is on GitHub.