ant: a Go database library that checks column names at compile time, without code generation
ant is a Go database library by its author that names columns with pointers to struct fields, for example ant.Gt(&Users.Age, 18). The compiler checks that the field exists and that the value has the right type, and no generated code is needed. GORM and Bun take column names as strings, so mistakes appear only when a query runs, while ent and sqlc catch them at build time but require a schema description and a generator[1][2].
Go 1.27 added generic methods, and the author says the chained API needs them. Without them, the API would have to use package-level functions that nest inside out. The compiler reports a misspelled field, a value of the wrong type, a misspelled relation, and a relation used as a value, and gopls shows these errors while the code is being typed[3].
To turn a pointer back into a column, ant finds the probe object whose memory range contains the address, subtracts its base, and matches the offset and type. A lookup takes about 21 ns with no allocations, measured over five runs on a Ryzen 5 3600 with a few probes registered. The approach depends on Go not moving heap objects, and the author plans to use runtime.Pinner to pin each probe. For writes, ant compares each loaded entity with a snapshot, so zero values such as 0 and false count as changes, unlike GORM's Updates, which skips zero-valued fields.
Read the original skudra.netDiscussion
Discussion
Commenters mostly welcome compile-time checks for queries. One says the approach shows the need for stronger generation tools, and another questions the allocations and locking in the benchmarks, which run in series rather than concurrently.
Sources
-
skudra.net
What Go 1.27’s generic methods made possible for ORMs. Compile-checked columns with no generated code
Terms
- probe — A helper object that ant creates only to get memory addresses of struct fields.


