Loading...
Loading...
WHEN: User is writing Go code, asking about Go patterns, reviewing Go code, asking "what's the best way to...", "how should I structure...", "is this idiomatic?", or any question about error handling, concurrency, interfaces, packages, testing patterns, or code organization in Go. Also activate when user is debugging Go code, refactoring Go, or working in a Go project (go.mod present) and asks general coding questions. Trigger this skill liberally for ANY Go-related development work. WHEN NOT: Non-Go languages, questions entirely unrelated to programming
npx skill4agent add gopherguides/gopher-ai go-best-practicesfmt.Errorf("operation failed: %w", err)var ErrNotFound = errors.New("not found")// Good
if err != nil {
return fmt.Errorf("failed to process user %s: %w", userID, err)
}
// Avoid
if err != nil {
log.Fatal(err) // Don't panic on recoverable errors
}// Good - interface defined by consumer
type Reader interface {
Read(p []byte) (n int, err error)
}
func ProcessData(r Reader) error { ... }
// Avoid - exporting implementation details
type Service interface {
Method1() error
Method2() error
Method3() error // Too many methods
}// Good - using errgroup
g, ctx := errgroup.WithContext(ctx)
for _, item := range items {
item := item // capture loop variable
g.Go(func() error {
return process(ctx, item)
})
}
if err := g.Wait(); err != nil {
return err
}func TestAdd(t *testing.T) {
tests := []struct {
name string
a, b int
want int
}{
{"positive numbers", 2, 3, 5},
{"with zero", 5, 0, 5},
{"negative numbers", -2, -3, -5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := Add(tt.a, tt.b)
if got != tt.want {
t.Errorf("Add(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.want)
}
})
}
}useruserServiceURLHTTPIDierrReadConfigRCinterface{}anyuser.UserServiceuser.Servicego vetstaticcheck