Skip to main content
ClaudeWave
Skill355 estrellas del repoactualizado today

go-best-practices

This Claude Code skill provides Go development patterns for type-first development practices. It should be applied when reading or writing Go files to ensure adherence to patterns including custom types for domain primitives, interface-based behavior contracts, functional options for configuration, and error handling strategies that leverage Go's type system to prevent invalid states at compile time.

Instalar en Claude Code
Copiar
git clone --depth 1 https://github.com/aiskillstore/marketplace /tmp/go-best-practices && cp -r /tmp/go-best-practices/skills/0xbigboss/go-best-practices ~/.claude/skills/go-best-practices
Después abre una sesión nueva de Claude Code; el skill carga automáticamente.

SKILL.md

# Go Best Practices

## Type-First Development

Types define the contract before implementation. Follow this workflow:

1. **Define data structures** - structs and interfaces first
2. **Define function signatures** - parameters, return types, and error conditions
3. **Implement to satisfy types** - let the compiler guide completeness
4. **Validate at boundaries** - check inputs where data enters the system

### Make Illegal States Unrepresentable

Use Go's type system to prevent invalid states at compile time.

**Structs for domain models:**
```go
// Define the data model first
type User struct {
    ID        UserID
    Email     string
    Name      string
    CreatedAt time.Time
}

type CreateUserRequest struct {
    Email string
    Name  string
}

// Functions follow from the types
func CreateUser(req CreateUserRequest) (*User, error) {
    // implementation
}
```

**Custom types for domain primitives:**
```go
// Distinct types prevent mixing up IDs
type UserID string
type OrderID string

func GetUser(id UserID) (*User, error) {
    // Compiler prevents passing OrderID here
}

func NewUserID(raw string) UserID {
    return UserID(raw)
}

// Methods attach behavior to the type
func (id UserID) String() string {
    return string(id)
}
```

**Interfaces for behavior contracts:**
```go
// Define what you need, not what you have
type Reader interface {
    Read(p []byte) (n int, err error)
}

type UserRepository interface {
    GetByID(ctx context.Context, id UserID) (*User, error)
    Save(ctx context.Context, user *User) error
}

// Accept interfaces, return structs
func ProcessInput(r Reader) ([]byte, error) {
    return io.ReadAll(r)
}
```

**Enums with iota:**
```go
type Status int

const (
    StatusActive Status = iota + 1
    StatusInactive
    StatusPending
)

func (s Status) String() string {
    switch s {
    case StatusActive:
        return "active"
    case StatusInactive:
        return "inactive"
    case StatusPending:
        return "pending"
    default:
        return fmt.Sprintf("Status(%d)", s)
    }
}

// Exhaustive handling in switch
func ProcessStatus(s Status) (string, error) {
    switch s {
    case StatusActive:
        return "processing", nil
    case StatusInactive:
        return "skipped", nil
    case StatusPending:
        return "waiting", nil
    default:
        return "", fmt.Errorf("unhandled status: %v", s)
    }
}
```

**Functional options for flexible construction:**
```go
type ServerOption func(*Server)

func WithPort(port int) ServerOption {
    return func(s *Server) {
        s.port = port
    }
}

func WithTimeout(d time.Duration) ServerOption {
    return func(s *Server) {
        s.timeout = d
    }
}

func NewServer(opts ...ServerOption) *Server {
    s := &Server{
        port:    8080,    // sensible defaults
        timeout: 30 * time.Second,
    }
    for _, opt := range opts {
        opt(s)
    }
    return s
}

// Usage: NewServer(WithPort(3000), WithTimeout(time.Minute))
```

**Embed for composition:**
```go
type Timestamps struct {
    CreatedAt time.Time
    UpdatedAt time.Time
}

type User struct {
    Timestamps  // embedded - User has CreatedAt, UpdatedAt
    ID    UserID
    Email string
}
```

## Module Structure

Prefer smaller files within packages: one type or concern per file. Split when a file handles multiple unrelated types or exceeds ~300 lines. Keep tests in `_test.go` files alongside implementation. Package boundaries define the API; internal organization is flexible.

## Functional Patterns

- Use value receivers when methods don't mutate state; reserve pointer receivers for mutation.
- Avoid package-level mutable variables; pass dependencies explicitly via function parameters.
- Return new structs/slices rather than mutating inputs; makes data flow explicit.
- Use closures and higher-order functions where they simplify code (e.g., `sort.Slice`, iterators).

## Instructions

- Return errors with context using `fmt.Errorf` and `%w` for wrapping. This preserves the error chain for debugging.
- Every function returns a value or an error; unimplemented paths return descriptive errors. Explicit failures are debuggable.
- Handle all branches in `switch` statements; include a `default` case that returns an error. Exhaustive handling prevents silent bugs.
- Pass `context.Context` to external calls with explicit timeouts. Runaway requests cause cascading failures.
- Reserve `panic` for truly unrecoverable situations; prefer returning errors. Panics crash the program.
- Add or update table-driven tests for new logic; cover edge cases (empty input, nil, boundaries).

## Examples

Explicit failure for unimplemented logic:
```go
func buildWidget(widgetType string) (*Widget, error) {
    return nil, fmt.Errorf("buildWidget not implemented for type: %s", widgetType)
}
```

Wrap errors with context to preserve the chain:
```go
out, err := client.Do(ctx, req)
if err != nil {
    return nil, fmt.Errorf("fetch widget failed: %w", err)
}
return out, nil
```

Exhaustive switch with default error:
```go
func processStatus(status string) (string, error) {
    switch status {
    case "active":
        return "processing", nil
    case "inactive":
        return "skipped", nil
    default:
        return "", fmt.Errorf("unhandled status: %s", status)
    }
}
```

Structured logging with slog:
```go
import "log/slog"

var log = slog.With("component", "widgets")

func createWidget(name string) (*Widget, error) {
    log.Debug("creating widget", "name", name)
    widget := &Widget{Name: name}
    log.Debug("created widget", "id", widget.ID)
    return widget, nil
}
```

## Configuration

- Load config from environment variables at startup; validate required values before use. Missing config should cause immediate exit.
- Define a Config struct as single source of truth; avoid `os.Getenv` scattered throughout code.
- Use sensible defaults for development; require explicit values for production secrets.

### Examples

Typed config struc
jira-safeSkill

Implement SAFe methodology in Jira. Use when creating Epics, Features, Stories with proper hierarchy, acceptance criteria, and parent-child linking.

jira-workflowSkill

Orchestrate Jira workflows end-to-end. Use when building stories with approvals, transitioning items through lifecycle states, or syncing task completion with Jira.

chinese-learning-assistantSkill

HSK4級レベルから流暢さを目指す学習者向け。中国語表現の使用場面・自然さを分析し、作文を「ネイティブらしい流暢な表現」に改善。bilibili等のコンテンツ理解とネイティブとの会話をサポート。実際の用例をWeb検索で提示

frontend-dev-guidelinesSkill

Next.js 15 애플리케이션을 위한 프론트엔드 개발 가이드라인. React 19, TypeScript, Shadcn/ui, Tailwind CSS를 사용한 모던 패턴. Server Components, Client Components, App Router, 파일 구조, Shadcn/ui 컴포넌트, 성능 최적화, TypeScript 모범 사례 포함. 컴포넌트, 페이지, 기능 생성, 데이터 페칭, 스타일링, 라우팅, 프론트엔드 코드 작업 시 사용.

skill-developerSkill

Claude Code 스킬, 훅, 에이전트, 명령어를 생성하고 관리하기 위한 메타 스킬. 새 스킬 생성, 스킬 트리거 설정, 훅 설정, Claude Code 인프라 관리 시 사용.

sitemapkitSkill

Discover and extract sitemaps from any website using SitemapKit. Use this skill whenever the user wants to find pages on a website, get a list of URLs from a domain, audit a site's structure, crawl a sitemap, check what pages exist on a site, or do anything involving sitemaps or site URL discovery — even if they don't explicitly say "sitemap". Requires the sitemapkit MCP server configured with a valid SITEMAPKIT_API_KEY.

create-prSkill

GitHubのプルリクエスト(PR)を作成する際に使用します。変更のコミット、プッシュ、PR作成を含む完全なワークフローを日本語で実行します。「PRを作って」「プルリクエストを作成」「pull requestを作成」などのリクエストで自動的に起動します。

create-svg-from-promptSkill

Generate an SVG of a user-requested image or scene