golang-database

Guides writing, reviewing, and debugging Go database code with sqlx and pgx.

Updated Jun 15, 2026
One-click install
npx skills add https://github.com/2877389577/novels_ai_gen --skill golang-database-2877389577
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-database
Source: https://github.com/2877389577/novels_ai_gen/tree/main/.agents/skills/golang-database
Command: npx skills add https://github.com/2877389577/novels_ai_gen --skill golang-database-2877389577

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go developers often write database code with subtle bugs: leaked connections from unclosed rows, SQL injection from string concatenation, missing context propagation, and unhandled sql.ErrNoRows cases. This Skill enforces safe, explicit database access patterns for PostgreSQL, MySQL, MariaDB, and SQLite without ORM magic. ## Core Features & Use Cases - Safe Query Patterns: Enforces parameterized queries, context propagation, explicit rows.Close(), and sql.ErrNoRows translation into domain errors. - Transactions & Locking: Covers BeginTxx/defer Rollback/Commit flows, isolation levels, and SELECT FOR UPDATE for race-condition-free reads. - Performance & Testing Guidance: Connection pool sizing, batch inserts of 100-1000 rows, pgx COPY protocol, cursor-based pagination, and integration tests with build tags and testcontainers. - Use Case: When asked to write a funds transfer function, the Skill produces a serializable transaction using SELECT FOR UPDATE with deferred rollback, rather than unsafe read-modify-write code. ## Quick Start Ask the agent to write or review a Go repository function that queries PostgreSQL with sqlx, and it will apply these database best practices 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 sql.ErrNoRows in Go database code?▼

Check with errors.Is(err, sql.ErrNoRows) and translate it into a domain error like ErrUserNotFound, or return an exists boolean. Wrap all other errors with fmt.Errorf and %w to preserve context.

How do I handle NULLable columns with sqlx struct scanning?▼

Use pointer fields like *string or *time.Time, which scan NULL as nil and marshal cleanly to JSON. sql.NullString works but requires custom JSON marshaling, and COALESCE in SQL is a third option.

How do I write a dynamic IN clause with sqlx?▼

Call sqlx.In to expand the placeholders for your slice, then call db.Rebind to convert the placeholders to your driver's format, and pass the returned args to SelectContext. Handle the error returned by sqlx.In.

Can this Skill generate a database schema or migration SQL?▼

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

Why should I avoid OFFSET pagination on large tables in Go?▼

OFFSET re-scans all skipped rows, making deep pages progressively slower at O(offset + limit). Use cursor-based pagination with WHERE created_at > $1 ORDER BY created_at LIMIT n, which stays O(limit) regardless of depth.