go

📁 poletron/custom-rules 📅 Jan 26, 2026
4
总安装量
3
周安装量
#48537
全站排名
安装命令
npx skills add https://github.com/poletron/custom-rules --skill go

Agent 安装分布

github-copilot 3
mcpjam 2
droid 2
antigravity 2
windsurf 2
zencoder 2

Skill 文档

Critical Patterns

Error Handling (REQUIRED)

// ✅ ALWAYS: Explicit error handling
result, err := doSomething()
if err != nil {
    return fmt.Errorf("doSomething failed: %w", err)
}

// ❌ NEVER: Ignore errors
result, _ := doSomething()

Naming Conventions (REQUIRED)

// ✅ ALWAYS: Short, clear names
func (u *User) Name() string { return u.name }

// Package-level: unexported unless needed
type userService struct { ... }  // internal
type UserService struct { ... }  // exported

// ❌ NEVER: Java-style names
func (user *User) GetUserName() string { ... }

Context Propagation (REQUIRED)

// ✅ ALWAYS: Pass context as first parameter
func GetUser(ctx context.Context, id string) (*User, error) {
    select {
    case <-ctx.Done():
        return nil, ctx.Err()
    default:
        return db.FindUser(ctx, id)
    }
}

Decision Tree

Need to share state?        → Use channels, not shared memory
Need cancellation?          → Use context.Context
Need cleanup?               → Use defer
Need interface?             → Define where used, not implemented
Need configuration?         → Use functional options

Code Examples

Functional Options

type ServerOption func(*Server)

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

func NewServer(opts ...ServerOption) *Server {
    s := &Server{port: 8080}
    for _, opt := range opts {
        opt(s)
    }
    return s
}

// Usage
server := NewServer(WithPort(9000))

Goroutines with Error Group

import "golang.org/x/sync/errgroup"

g, ctx := errgroup.WithContext(ctx)

g.Go(func() error {
    return fetchUsers(ctx)
})

g.Go(func() error {
    return fetchOrders(ctx)
})

if err := g.Wait(); err != nil {
    return err
}

Commands

go mod init myapp            # Initialize module
go run .                     # Run application
go build -o bin/app .        # Build binary
go test ./...                # Run all tests
go test -race ./...          # Race detection
golangci-lint run            # Linting

Resources