AgentSkillsCN

patterns

项目模式包括函数式选项、必须模式、缓存装饰器、错误处理、配置管理,以及 HTTP 客户端。适用于在代码库中实现常见模式时使用。

SKILL.md
--- frontmatter
name: patterns
description: Project patterns including functional options, must pattern, caching decorator, error handling, configuration, and HTTP clients. Use when implementing common patterns in the codebase.
userInvokable: true

Project Patterns

References: Examples

Functional Options

Example

Use With* functions for configurable constructors:

go
func NewClient(baseURL string, options ...ClientOption) *Client {
    opts := defaultOptions()
    for _, option := range options {
        option(opts)
    }
    // create client...
}

client := NewClient(url, WithTimeout(30*time.Second), WithRetryCount(3))

Must Pattern

Example

Allowed only in cmd/, internal/apps/ - to fail fast on misconfiguration.

go
func Must[T any](f func() (T, error)) T {
    v, err := f()
    if err != nil { panic(err) }
    return v
}

Caching Decorator

Example

Wrap clients with cache layer for transparent caching.

Error Handling

Example

RuleExample
Wrap with contextfmt.Errorf("get order %s: %w", id, err)
Check with Is/Aserrors.Is(err, ErrNotFound)
Handle onceDon't log AND return
Custom errorsvar ErrFoo = errors.New() or type FooError struct{}

Configuration

Example

TypePath
App configconfig/appconfig/{env}.json
Infra configconfig/appconfig/infra-{env}.json
SSM/myservice/{env}/app/config

Rules: No ARNs in app config, no secrets in files, use time.Duration with custom unmarshaler

HTTP Client with Retry

Example

go
client := tracing.NewRestyClient(baseURL,
    tracing.WithTimeout(30*time.Second),
    tracing.WithRetryCount(3),
    tracing.WithExponentialBackoff(true),
)

DynamoDB Key Design

Example

Use composite keys (PK + SK), GSIs for query patterns.

Feature Flags

Example

Fallback defaults, check at service layer, log evaluations.

JSON Tags

Example

Use camelCase for JSON tags and query params.

Request Validation

Example

Use ozzo-validation for struct validation.

Libraries

LibraryPurpose
chiHTTP router
samber/loCollections/utilities
ozzo-validationValidation
restyHTTP client