golang-database-patterns

Implement Go database integration patterns with sqlx, pgx, and golang-migrate.

Updated Feb 1, 2026
One-click install
npx skills add https://github.com/onlyspans/events --skill golang-database-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-database-patterns
Source: https://github.com/onlyspans/events/tree/main/.agents/skills/golang-database-patterns
Command: npx skills add https://github.com/onlyspans/events --skill golang-database-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill provides comprehensive guidance and practical examples for interacting with SQL databases in Go, covering everything from basic queries to advanced patterns like connection pooling, transaction management, and schema migrations.

Core Features & Use Cases

  • Database Library Selection: Guides you in choosing between database/sql, sqlx, and pgx based on your needs.
  • Repository Pattern: Demonstrates how to implement an interface-based repository pattern for testable and maintainable data access.
  • Migrations: Explains how to use golang-migrate for robust database schema versioning.
  • Transaction Handling: Covers safe transaction management, isolation levels, and retry logic.
  • Use Case: When building a new Go web service that requires a PostgreSQL backend, use this Skill to set up your database connection, implement user data CRUD operations using sqlx, and manage your schema with golang-migrate.

Quick Start

Use the golang-database-patterns skill to set up a connection pool using pgx and implement a user repository with Create, GetByID, and Update methods.

Frequently Asked Questions about golang-database-patterns

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

FAQPage Schema
How do I implement the repository pattern in Go for database access?

The repository pattern in Go is implemented using interface-based data access layers to ensure your database queries are testable and maintainable. This approach abstracts SQL operations behind interfaces, allowing easy mocking and clean separation of concerns.

What is the best way to choose between sqlx and pgx for a Go web service?

Choosing between sqlx and pgx depends on your PostgreSQL integration depth. sqlx extends the standard database/sql package with marshaling helpers, while pgx offers a specialized driver with advanced features like connection pooling and type-safe querying for PostgreSQL-specific workloads.

How do I manage database schema migrations using golang-migrate?

Database schema migrations using golang-migrate are managed through versioned SQL files that track incremental changes to your database structure. This provides robust schema versioning, allowing you to apply or roll back migrations systematically during deployment.

How do you handle transactions and connection pooling in Go database integration?

Transaction handling and connection pooling in Go database integration are managed by configuring the pool limits and using safe transaction blocks. This includes setting isolation levels, implementing retry logic for failed queries, and ensuring connections are properly returned to the pool.

Can I use pgx for type-safe querying in a PostgreSQL backend?

Yes, you can use pgx for type-safe querying in a PostgreSQL backend. pgx provides specialized type mapping and encoding capabilities that reduce runtime errors during data marshaling compared to standard library database interfaces.

Why does my Go data access layer lack testability and how can I fix it?

A Go data access layer lacks testability when database logic is tightly coupled to business logic. You can fix this by adopting an interface-based repository pattern, which abstracts data operations and allows you to inject mock repositories during unit testing.