| name | go-best-practices |
| description | Go development best practices. Idiomatic Go, error handling, concurrency patterns, testing. Use when writing Go code. |
| compatibility | opencode |
Go Best Practices
Follow Go proverbs and community standards for all Go code.
Core Principles
- Simplicity: Go is simple. Avoid over-engineering.
- Idiomatic: Follow effective Go guidelines
- Performance: Profile before optimizing
- Testing: Test-driven development
Error Handling
if err != nil {
return fmt.Errorf("failed to do operation: %w", err)
}
var NotFoundError struct{ Name string }
func (e NotFoundError) Error() string {
return fmt.Sprintf("not found: %s", e.Name)
}
Concurrency
jobs := make(chan Job, workerCount)
results := make(chan Result, workerCount)
for i := 0; i < workerCount; i++ {
go worker(jobs, results)
}
Context
func DoSomething(ctx context.Context, arg Arg) (Result, error) {
req, err := http.NewRequestWithContext(ctx, ...)
}
Testing
tests := []struct {
name string
input Arg
expected Result
}{
{"case 1", Arg{...}, Expected{...}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := DoSomething(tt.input)
assert.Equal(t, tt.expected, result)
})
}
Build Commands
go fmt ./...
go vet ./...
golangci-lint run
go test -race -coverprofile=coverage.out ./...
go build -o binary ./cmd/...
Quality Gates