A code-generated, type-safe SQL query builder and struct mapper for Go that eliminates magic strings and boilerplate.
sq (Structured Query) is a Go library that provides a type-safe query builder and struct mapper on top of database/sql. It generates Go structs from your database schema to eliminate magic strings in queries and automate the mapping of SQL results to Go variables, reducing boilerplate code. The library focuses on making SQL querying in Go pleasant and efficient by improving null handling and ensuring queries reflect the actual database structure.
Go developers working with PostgreSQL or MySQL who need a type-safe, non-ORM way to build SQL queries and map results directly to structs without manual row scanning. It is particularly suited for projects where database schema changes are frequent and safety from typos is critical.
Developers choose sq over alternatives because it generates type-safe table and column definitions from the database, eliminating magic strings and schema mismatches. Its unique selling point is that the SELECT clause directly maps to Go variables automatically, removing the need for repetitive scanning code or struct tags, while providing better null handling by scanning NULLs as zero values without requiring sql.NullXXX structs or pointers.
Type safe SQL query builder and struct mapper for Go
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Generates Go table structs directly from the database schema, eliminating magic strings and preventing typos, as highlighted in the README's emphasis on avoiding schema mismatches.
The SELECT clause maps directly to Go variables without manual row scanning or column ordering, reducing boilerplate—the README states 'Write your query, run it, you're done.'
Scans NULL database values as zero values in Go while allowing NULL checks, avoiding the need for sql.NullXXX structs or pointers, which the README cites as a pain point in standard Go.
Supports advanced SQL features like CTEs, window functions, subqueries, and dialect-specific options such as PostgreSQL's DISTINCT ON, as listed in the Features section.
The README explicitly states development has stopped with no new features added, directing users to a successor library, which limits future enhancements and support.
Requires running a separate command-line tool (sqgen-postgres or sqgen-mysql) to generate table structs, adding complexity to setup and maintenance compared to runtime-only libraries.
Only enforces type safety on method calls; SQL semantics are not checked until runtime by the database, meaning errors like misusing ASC in SELECT clauses (as noted in the README) can slip through.