go-database

Write and review explicit Go database code with database/sql, sqlx, and pgx.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill go-database-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-database
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/go/go-database
Command: npx skills add https://github.com/asarchami/dotfiles --skill go-database-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go database code often hides injection risks, leaked connections, unhandled ErrNoRows cases, and unconfigured connection pools. This Skill enforces explicit, parameterized SQL with proper resource management, error translation, and transaction boundaries so integrity bugs do not reach production. ## Core Features & Use Cases - Explicit SQL patterns: Parameterized queries with driver placeholders, sqlx.In for dynamic IN clauses, and allowlists for dynamic column names across PostgreSQL, MySQL, MariaDB, and SQLite. - Resource and error discipline: Mandatory rows.Close(), rows.Err() checks, ErrNoRows translation to domain errors, and context propagation through every call. - Transactions and performance: BeginTx patterns, isolation levels, SELECT FOR UPDATE locking, connection pool tuning, batch sizing, and cursor-based pagination. - Use Case: When reviewing a Go service that queries PostgreSQL, use this Skill to catch string-concatenated SQL, missing context propagation, and default pool settings before they cause incidents. ## Quick Start Ask the AI to review your Go repository layer for SQL injection risks, connection leaks, and missing transaction boundaries using the go-database skill.

Frequently Asked Questions about go-database

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

FAQPage Schema
How do I write parameterized SQL queries in Go?

Use driver placeholders instead of string concatenation: $1 for PostgreSQL and ? for MySQL, passing values as arguments to QueryContext or GetContext. For dynamic IN clauses use sqlx.In with Rebind, and allowlist dynamic column names through a map lookup.

sqlx vs pgx vs database/sql: which Go database library should I use?

Use database/sql for portability and minimal dependencies, sqlx for multi-database struct scanning with StructScan, and pgx for PostgreSQL-specific features like COPY, LISTEN, and arrays with 30-50% better performance. ORMs like GORM are discouraged because they hide generated SQL.

How do I handle NULLable columns when scanning into Go structs?

Use pointer fields like *string or *time.Time, which scan NULL as nil and marshal cleanly to JSON. Alternatives are sql.NullString types or COALESCE in the SQL query, but pointers are the recommended approach.

Why does my Go database code leak connections?

Leaks happen when rows.Close() is missing after QueryContext or when db.Query is used for statements returning no rows. Always defer rows.Close() immediately after querying, check rows.Err() after iteration, and use db.Exec for row-less statements.

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

Define a repository interface and mock it with testify/mock for unit tests, or use DATA-DOG/go-sqlmock to verify exact SQL. For integration tests, use build tags with testcontainers-go to spin up a real PostgreSQL container in CI.

Does this skill generate database schemas or migration SQL?

No, schema creation and migration SQL are explicitly out of scope. Use external tools like golang-migrate, Flyway, or Atlas, where migrations are written by humans, versioned in source control, and applied through CI/CD.