golang-database

Generate and audit Go database access code using sqlx or pgx.

2|Updated Feb 12, 2024
One-click install
npx skills add https://github.com/adibfirman/dotfiles --skill golang-database-adibfirman
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-database
Source: https://github.com/adibfirman/dotfiles/tree/main/claude/.claude/skills/technical/golang/golang-database
Command: npx skills add https://github.com/adibfirman/dotfiles --skill golang-database-adibfirman

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you avoid common and costly database bugs in Go by enforcing safe SQL practices, correct resource handling, and production-grade patterns for queries, transactions, NULL handling, and migrations.

Core Features & Use Cases

  • Parameterized queries & SQL injection safety: Ensures user input is never concatenated into SQL and promotes allowlisting for dynamic column names.
  • Correct scanning and NULL handling: Guides struct scanning with sqlx/pgx and recommends pointer fields or sqlx-friendly patterns for nullable columns.
  • Robust correctness & reliability: Covers explicit sql.ErrNoRows handling, mandatory rows.Close(), rows.Err() checks, and context propagation with *Context methods.
  • Concurrency-safe writes: Recommends transactions with appropriate isolation levels and SELECT ... FOR UPDATE when you plan to update what you read.
  • Operational readiness: Includes connection pool configuration and batching guidance, and explicitly refuses schema generation.

Quick Start

Use the golang-database skill to review or generate a Go function that performs a PostgreSQL query with parameterized placeholders, propagates ctx, handles NULLable columns correctly, and safely iterates rows while checking rows.Err().

Frequently Asked Questions about golang-database

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

FAQPage Schema
How do I handle NULL values when scanning database rows in Go using sqlx or pgx?

To handle NULL values in Go database scanning, use pointer fields within your structs or utilize sqlx-friendly scanning patterns to safely capture nullable columns without triggering runtime panic errors.

What is the best way to manage database transactions and locking in Go without an ORM?

The best way to manage database transactions in Go is by explicitly setting transaction boundaries with appropriate isolation levels and using SELECT FOR UPDATE locking patterns when you need to update data you just read.

Why does my Go database query leak connections when using sqlx or pgx?

Your Go database query likely leaks connections because rows.Close() is not called immediately after iteration, and you must also check rows.Err() to ensure the query loop completed without silent execution failures.

How do I write safe parameterized SQL queries in Go to prevent SQL injection?

To write safe parameterized SQL queries in Go, ensure user input is never concatenated directly into SQL strings, use strict parameterized placeholders, and implement allowlisting for any dynamic column names.

Can I use this approach to generate database schema migrations for PostgreSQL and MariaDB?

No, this approach explicitly refuses schema generation and creation, requiring you to use external migration tooling to manage database schemas for PostgreSQL, MariaDB, MySQL, or SQLite environments.

Do I need to pass context to every Go database function when using pgx or sqlx?

Yes, you need to pass context to every database function by using *Context method variants to propagate timeouts and cancellations correctly, ensuring your connection pool operations remain responsive and safe.