Agent Skills: Golang Database

Implement database access with connection pooling and repository patterns in Go. Use when building database access, connection pools, or repositories in Go. (triggers: internal/adapter/repository/**, database, sql, postgres, gorm, sqlc, pgx)

UncategorizedID: hoangnguyen0403/agent-skills-standard/golang-database

Install this agent skill to your local

pnpm dlx add-skill https://github.com/HoangNguyen0403/agent-skills-standard/tree/HEAD/skills/golang/golang-database

Skill Files

Browse the full folder contents for golang-database.

Download Skill

Loading file tree…

skills/golang/golang-database/SKILL.md

Skill Metadata

Name
golang-database
Description
"Implement database access with connection pooling and repository patterns in Go. Use when building database access, connection pools, or repositories in Go. (triggers: internal/adapter/repository/**, database, sql, postgres, gorm, sqlc, pgx)"

Golang Database

Priority: P0 (CRITICAL)

Principles

  • Prefer Raw SQL/Builders over ORMs: sqlc generates type-safe Go from SQL. ORMs (GORM) can obscure performance.
  • Repository Pattern: Abstract DB access behind interfaces in internal/port/.
  • Connection Pooling: Always configure pool settings.
  • Transactions: ACID logic must use transactions. Pass context.Context everywhere.

Implementation Workflow

  1. Choose driver — PostgreSQL: pgx/v5; MySQL: go-sql-driver/mysql; SQLite: modernc.org/sqlite.
  2. Configure pool — Set MaxOpenConns, MaxIdleConns, and ConnMaxLifetime on the connection.
  3. Define repository interface — Abstract DB access behind an interface at the consumer side.
  4. Use context-aware queries — Always use QueryContext/ExecContext; bare queries ignore timeouts.
  5. Close rows — Always defer rows.Close() and check rows.Err() after iteration.
  6. Wrap in transactions — Use transactions for multi-step operations requiring atomicity.

See repository pattern and connection pool examples

Anti-Patterns

  • ❌ Global var db *sql.DB — inject DB connection via constructor
  • ❌ Bare Query()/Exec() without context — use QueryContext/ExecContext
  • ❌ Leaked rows — always defer rows.Close() and check rows.Err()

References