AgentSkillsCN

go-best-practices

Go 编程的最佳实践。当用户编写或审查 Go 代码时,可使用此技能。涵盖错误处理、并发编程以及符合 Go 语言风格的常用模式。

SKILL.md
--- frontmatter
name: go-best-practices
description: Go coding best practices. Use when writing or reviewing Go code. Covers error handling, concurrency, and idiomatic patterns.
license: MIT
metadata:
  author: plan-cascade
  version: "1.0.0"

Go Best Practices

Code Style

RuleGuideline
Formattergofmt or goimports
Lintergolangci-lint
NamingShort, clear; avoid stuttering
CommentsGodoc for exported items

Error Handling

RuleGuideline
Always checkNever ignore errors
Wrap contextfmt.Errorf("ctx: %w", err)
Sentinel errorsvar ErrNotFound = errors.New(...)
go
func Load(path string) (*Config, error) {
    data, err := os.ReadFile(path)
    if err != nil {
        return nil, fmt.Errorf("load config %s: %w", path, err)
    }
    // ...
}

Project Structure

code
cmd/appname/main.go
internal/config/
internal/service/
go.mod

Concurrency

PatternUsage
context.ContextCancellation, timeouts
sync.WaitGroupWait for goroutines
errgroup.GroupGoroutines with errors
go
func Process(ctx context.Context, items []Item) error {
    for _, item := range items {
        select {
        case <-ctx.Done():
            return ctx.Err()
        default:
            if err := process(item); err != nil { return err }
        }
    }
    return nil
}

Anti-Patterns

AvoidUse Instead
Naked returnsExplicit returns
panic for errorsReturn errors
Large interfacesSmall, focused
init()Explicit init

Testing (Table-Driven)

go
func TestParse(t *testing.T) {
    tests := []struct{ name, input string; want int; wantErr bool }{
        {"valid", "42", 42, false},
        {"invalid", "abc", 0, true},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got, err := Parse(tt.input)
            if (err != nil) != tt.wantErr { t.Errorf("err=%v, want=%v", err, tt.wantErr) }
            if got != tt.want { t.Errorf("got=%v, want=%v", got, tt.want) }
        })
    }
}