Go database operations - SQL, ORMs, transactions, migrations
View on GitHubpluginagentmarketplace/custom-plugin-go
go-development-assistant
January 21, 2026
Select agents to install to:
npx add-skill https://github.com/pluginagentmarketplace/custom-plugin-go/blob/main/skills/go-database/SKILL.md -a claude-code --skill go-databaseInstallation paths:
.claude/skills/go-database/# Go Database Skill
Production database patterns with Go including SQL, ORMs, and data access layer design.
## Overview
Best practices for database operations covering connection pooling, transactions, migrations, and query optimization.
## Parameters
| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| database | string | yes | - | Database: "postgres", "mysql", "sqlite" |
| orm | string | no | "sqlx" | ORM: "none", "sqlx", "gorm" |
| pool_size | int | no | 25 | Max open connections |
## Core Topics
### Connection Setup
```go
func NewDB(dsn string) (*sqlx.DB, error) {
db, err := sqlx.Connect("postgres", dsn)
if err != nil {
return nil, fmt.Errorf("connect: %w", err)
}
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(5)
db.SetConnMaxLifetime(5 * time.Minute)
db.SetConnMaxIdleTime(1 * time.Minute)
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("ping: %w", err)
}
return db, nil
}
```
### Repository Pattern
```go
type UserRepository struct {
db *sqlx.DB
}
func (r *UserRepository) FindByID(ctx context.Context, id int64) (*User, error) {
var user User
err := r.db.GetContext(ctx, &user,
`SELECT id, name, email, created_at FROM users WHERE id = $1`, id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrUserNotFound
}
return nil, fmt.Errorf("find user %d: %w", id, err)
}
return &user, nil
}
func (r *UserRepository) Create(ctx context.Context, user *User) error {
query := `INSERT INTO users (name, email) VALUES ($1, $2) RETURNING id, created_at`
return r.db.QueryRowxContext(ctx, query, user.Name, user.Email).
Scan(&user.ID, &user.CreatedAt)
}
```
### Transactions
```go
func (r *OrderRepository) CreateOrder(ctx context.Context, order *Order, items []OrderItem) error {
tx, err := r.db.BeginTxx(ctx, nil)
if err != nil {
return fmt.E