← ClaudeAtlas

go-databaselisted

Use when writing, reviewing, or debugging Go code that talks to a SQL database (PostgreSQL, MySQL, MariaDB, SQLite). Covers library choice (database/sql, sqlx, sqlc, pgx, GORM trade-offs), parameterized queries, context propagation, NULL handling, scanning, transactions and isolation, connection pool tuning, and migration tooling. Apply when adding repository code, refactoring SQL, or auditing for missing rows.Close()/QueryContext.
muratmirgun/gophers · ★ 5 · API & Backend · score 83
Install: claude install-skill muratmirgun/gophers
# Go Database Go's `database/sql` is a thin, driver-pluggable foundation. Most projects layer one of `sqlx`, `sqlc`, or `pgx` on top for ergonomics. ORMs (GORM, ent) trade SQL visibility for one less line of code — a bad trade in production. ## Core Rules 1. **SQL is the source of truth.** It is reviewed, version-controlled, and explained in code. Magic ORM queries are the opposite. 2. **Always parameterize.** `$1`/`?` placeholders, never string concatenation. The driver handles escaping; you cannot. 3. **Every I/O call takes `ctx`.** `QueryContext`, `ExecContext`, `GetContext`. No context = no timeout = a stuck handler. 4. **Distinguish "not found" from "error".** `errors.Is(err, sql.ErrNoRows)` is a domain signal, not a failure. 5. **Close rows.** `defer rows.Close()` immediately after `QueryContext`. Forgetting it leaks a pool connection. 6. **Configure the pool.** Default `MaxOpenConns` is unlimited — a runaway request rate exhausts the DB. ## Library Decision | Library | Best for | Struct scanning | Code-gen | |---|---|---|---| | `database/sql` | Minimal deps, multi-driver portability | Manual `Scan` | No | | `sqlx` | Sweetens `database/sql` ergonomics | `StructScan`, `Get`, `Select` | No | | `sqlc` | Type-safe queries derived from `.sql` files | Generated structs and funcs | Yes | | `pgx` (v5) | PostgreSQL-only, 30-50% faster, native types | `pgx.RowToStructByName` | No | | GORM / ent | **Avoid** in new code | Reflection | Yes | **Why not ORMs.** - Generated quer