AGENT SKILL
golang-database
Standards for database interaction, connection pooling, and repository patterns in Golang. Use when implementing database access, connection pools, or repositories in Go. (triggers: internal/adapter/repository/**, database, sql, postgres, gorm, sqlc, pgx)
- License 许可
- MIT
- 最近更新
- 2026-06-24
- Stars
- ★ 67
- Forks
- 24
01 前言 Front Matter
- name
- golang-database
- description
- Standards for database interaction, connection pooling, and repository patterns in Golang. Use when implementing database access, connection pools, or repositories in Go. (triggers: internal/adapter/repository/**, database, sql, postgres, gorm, sqlc, pgx)
02 资源文件 4
- 参考文档.claude-plugin/plugin.json293 B
- 参考文档references/fhir_mappings.md7.1 KB
- 参考文档references/schema.md10.3 KB
- 脚本scripts/health_query.py29.6 KB
03 SKILL.md 正文
Golang Database Standards
Priority: P0 (CRITICAL)
Principles
- Prefer Raw SQL/Builders over ORMs: Go structs map well to SQL. ORMs (GORM) can obscure performance. Recommended:
sqlc(type-safe SQL generation). - Repository Pattern: Abstract DB access behind interfaces in
internal/port/(e.g.,UserRepository). - Connection Pooling: Always configure
SetMaxOpenConns,SetMaxIdleConns,SetConnMaxLifetime. - Transactions: Logic requiring ACID MUST use transactions. Pass
context.Contexteverywhere.
Drivers
- PostgreSQL:
jackc/pgx(Preferpgx/v5for performance and features). - MySQL:
go-sql-driver/mysql. - SQLite:
mattn/go-sqlite3ormodernc.org/sqlite(pure Go).
Anti-Patterns
- No global db var: Inject DB connection via constructor; never use
var db *sql.DB. - No bare queries: Use
QueryContext/ExecContext; bare queries ignore context timeouts. - No leaked rows: Always
defer rows.Close()and checkrows.Err()after iteration.