go-database
Skill72 starsChanged 4 months agoMITView on GitHub
Database patterns for Go services: database/sql, connection management, transactions, migrations, query builders, and ORM usage (sqlc, GORM, ent). Use when: "database access", "SQL query", "connection pool", "transactions", "database migration", "sqlc", "GORM", "ent", "prepared statement", "repository pattern". Not for: in-memory structures (go-data-structures), SQL security (go-security-audit), query profiling (go-performance-review).
1 other project uses a version of this.
Adopted by
Most recently taken up by
- muratmirgun/gopherstheir own version, first seen Aug 11, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- muratmirgun/gophers★ 9same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository36 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- .cursor/rules/go-skills.mdcCursor rule
- go-api-designSkill
- go-architecture-reviewSkill
- go-cliSkill
- go-dependency-injectionSkill
- go-design-patternsSkill
- go-graphqlSkill
- go-grpcSkill
- go-interface-designSkill
- go-openapiSkill
- go-project-layoutSkill
- go-code-reviewSkill
- go-coding-standardsSkill
- go-contextSkill
- go-data-structuresSkill
- go-documentationSkill
- go-error-handlingSkill
- go-modernizeSkill
- go-concurrency-reviewSkill
- go-defensive-codingSkill
- go-observabilitySkill
- go-performance-reviewSkill
- go-security-auditSkill
- go-troubleshootingSkill
- go-test-qualitySkill
- go-test-table-drivenSkill
- git-commitSkill
- go-binary-sizeSkill
- go-ciSkill
- go-dependency-auditSkill
- go-refactoringSkill
- go-semantic-toolsSkill
- go-skills-routerSkill
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 5 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 32
Read a reference file only when the summary below is not enough.
- Distinctive: line 95
Use a helper that guarantees rollback on error:
- Distinctive: line 126
sqlc: prefer it for raw-SQL projects — generates type-safe Go from annotated SQL, catching query/schema mismatches at build time.
- Very specific: line 128
Migrations: use a tool (goose, golang-migrate, atlas), one migration per change, forward-only in production, with down SQL, run as a …
- Very specific: line 136
Null columns: use sql.NullString/sql.NullInt64 or pointer fields (*string, nil = SQL NULL).
- Distinctive: line 149
All queries use parameterized placeholders, never string concatenation
- Distinctive: line 156
No N+1 query patterns — use JOINs or batch queries
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Go Database Patterns
Database access is where most Go services spend their complexity budget. Get connection management, transactions, and query patterns right.
Detailed reference material, loaded on demand:
references/query-patterns.md— full query/scan/rows patterns, null handling, N+1 avoidance, connection-leak examples.references/tooling.md— repository pattern implementation, sqlc annotated queries, migration tooling and rules.
Read a reference file only when the summary below is not enough.
1. Connection Management
Configure the pool explicitly — the default is unbounded connections:
func OpenDB(dsn string) (*sql.DB, error) {
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, fmt.Errorf("open db: %w", err)
}
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(10)
db.SetConnMaxLifetime(5 * time.Minute)
db.SetConnMaxIdleTime(1 * time.Minute)
if err := db.PingContext(context.Background()); err != nil {
return nil, fmt.Errorf("ping db: %w", err)
}
return db, nil
}| Setting | Guideline |
MaxOpenConns | Match your DB's max connections / number of app instances |
MaxIdleConns | 40-50% of MaxOpenConns |
ConnMaxLifetime | 5-10 minutes (prevents stale connections behind load balancers) |
ConnMaxIdleTime | 1-2 minutes |
2. Query Rules
- Parameterized queries only — string concatenation into SQL is an injection vulnerability, no exceptions.
- Always pass context — use the
*Contextvariants (QueryContext,QueryRowContext,ExecContext) so queries respect cancellation and timeouts. defer rows.Close()immediately after the error check, and checkrows.Err()after the iteration loop.- Handle
sql.ErrNoRowsexplicitly witherrors.Is, mapping it to a domain error likeErrUserNotFound.
var user User
err := db.QueryRowContext(ctx,
"SELECT id, name, email FROM users WHERE id = $1", id,
).Scan(&user.ID, &user.Name, &user.Email)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrUserNotFound
}
if err != nil {
return nil, fmt.Errorf("get user %s: %w", id, err)
}Multi-row iteration patterns: references/query-patterns.md.
3. Transactions
Use a helper that guarantees rollback on error:
func WithTx(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
if err := fn(tx); err != nil {
if rbErr := tx.Rollback(); rbErr != nil {
return fmt.Errorf("rollback failed: %v (original: %w)", rbErr, err)
}
return err
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit tx: %w", err)
}
return nil
}Set isolation explicitly for critical operations:
sql.TxOptions{Isolation: sql.LevelSerializable}.
4. Structure and Tooling
- Repository pattern: define the interface at the consumer side, implement it with concrete database access, map driver errors to domain errors at this boundary.
- sqlc: prefer it for raw-SQL projects — generates type-safe Go from annotated SQL, catching query/schema mismatches at build time.
- Migrations: use a tool (
goose,golang-migrate,atlas), one migration per change, forward-only in production, withdownSQL, run as a separate step — not at server startup.
Implementations and examples: references/tooling.md.
5. Common Pitfalls
- Null columns: use
sql.NullString/sql.NullInt64or pointer fields (*string, nil = SQL NULL). Scanning NULL into a plain string errors at runtime. - N+1 queries: a query inside a loop over query results. Replace
with a JOIN or a batch query (
WHERE id = ANY($1)). - Connection leaks: any early
returnbetweenQueryanddefer rows.Close()leaks a connection from the pool.
Worked examples of each pitfall: references/query-patterns.md.
Verification Checklist
- Connection pool configured with explicit limits (
MaxOpenConns,MaxIdleConns, lifetimes) - All queries use parameterized placeholders, never string concatenation
- All
QueryContextresults havedefer rows.Close()immediately after error check rows.Err()checked after row iteration loopsql.ErrNoRowshandled explicitly witherrors.Is- Transactions use a helper that guarantees rollback on error
- Context propagated to all database calls (
*Contextvariants) - Nullable columns use
sql.NullString/sql.NullInt64or pointer types - No N+1 query patterns — use JOINs or batch queries
- Migrations are versioned, reversible, and run separately from app startup
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

