golang-database

Guides writing, reviewing, and debugging Go database code using sqlx, pgx, and database/sql.

1|Updated May 25, 2020
One-click install
npx skills add https://github.com/titaneric/dotfiles --skill golang-database-titaneric
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-database
Source: https://github.com/titaneric/dotfiles/tree/main/dot_agents/skills/golang-database
Command: npx skills add https://github.com/titaneric/dotfiles --skill golang-database-titaneric

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go developers frequently ship database code with connection leaks, SQL injection risks, unhandled NULL values, and missing transaction boundaries. This Skill enforces safe, explicit database access patterns so these defects are caught at the code level rather than in production. ## Core Features & Use Cases - Safe Query Patterns: Enforces parameterized queries, context propagation, explicit rows.Close(), and sql.ErrNoRows translation into domain errors. - Transactions and Locking: Covers BeginTxx/defer Rollback/Commit flows, isolation levels, and SELECT FOR UPDATE for race-condition prevention. - Performance and Testing Guidance: Provides connection pool sizing, batch processing (100-1000 rows), cursor-based pagination, pgx COPY bulk inserts, and integration tests with build tags and testcontainers. - Use Case: When asked to write a funds-transfer function in Go, the Skill produces a serializable transaction that locks account rows with SELECT FOR UPDATE, defers rollback, and wraps errors with context. ## Quick Start Ask the agent to write or review a Go repository function that queries PostgreSQL with sqlx, and it will apply parameterized queries, context propagation, and proper error handling automatically.

Frequently Asked Questions about golang-database

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Should I use GORM, sqlx, or pgx for a Go PostgreSQL project?

Use pgx for PostgreSQL-only projects since it is 30-50% faster and supports COPY, LISTEN, and arrays; use sqlx for multi-database projects. Avoid ORMs like GORM because they generate unpredictable queries, hide N+1 problems, and make debugging harder.

How do I handle NULLable columns in Go structs with sqlx?

Use pointer fields such as *string or *time.Time, which scan NULL values cleanly and marshal to JSON naturally. sql.NullString works but requires custom JSON marshaling, and COALESCE in SQL is a third option that moves NULL handling into the query.

How do I write a dynamic IN clause with sqlx?

Use sqlx.In to expand the placeholders for a slice of values, then call db.Rebind to adjust placeholder syntax for your driver. Pass the expanded args to a Context method like SelectContext and handle the error returned by sqlx.In.

Why should I avoid OFFSET pagination on large tables?

OFFSET re-scans all skipped rows, making deep pages progressively slower with O(offset + limit) cost. Cursor-based pagination using WHERE created_at > $1 with ORDER BY and LIMIT stays O(limit) regardless of page depth.

Can this Skill generate a database schema or migration SQL?

No. The Skill explicitly refuses to generate schemas or migration SQL because AI-generated schemas often miss indexes, use incorrect types, or ignore production data volumes. Use golang-migrate, Flyway, or Atlas with human review instead.

How do I test Go database code without a production database?

Use interface mocks or go-sqlmock for unit tests, and gate integration tests behind a //go:build integration tag running against testcontainers-go or a test DSN. Wrap each integration test in a transaction that rolls back for isolation.