Agent-skills-standard 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)
install
source · Clone the upstream repo
git clone https://github.com/HoangNguyen0403/agent-skills-standard
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/HoangNguyen0403/agent-skills-standard "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/golang/golang-database" ~/.claude/skills/hoangnguyen0403-agent-skills-standard-golang-database && rm -rf "$T"
manifest:
skills/golang/golang-database/SKILL.mdsource content
Golang Database
Priority: P0 (CRITICAL)
Principles
- Prefer Raw SQL/Builders over ORMs:
generates type-safe Go from SQL. ORMs (GORM) can obscure performance.sqlc - Repository Pattern: Abstract DB access behind interfaces in
.internal/port/ - Connection Pooling: Always configure pool settings.
- Transactions: ACID logic must use transactions. Pass
everywhere.context.Context
Implementation Workflow
- Choose driver — PostgreSQL:
; MySQL:pgx/v5
; SQLite:go-sql-driver/mysql
.modernc.org/sqlite - Configure pool — Set
,MaxOpenConns
, andMaxIdleConns
on connection.ConnMaxLifetime - Define repository interface — Abstract DB access behind interface at consumer side.
- Use context-aware queries — Always use
/QueryContext
; bare queries ignore timeouts.ExecContext - Close rows — Always
and checkdefer rows.Close()
after iteration.rows.Err() - Wrap in transactions — Use transactions for multi-step operations requiring atomicity.
See repository pattern and connection pool examples
Anti-Patterns
- No global db var: inject DB connection via constructor.
- No context-less queries: use
/QueryContext
; bare queries ignore timeouts.ExecContext - No leaked rows: always
and checkdefer rows.Close()
.rows.Err()