Loading...
Loading...
Go Development Guidelines, including naming conventions, error handling, concurrent programming, testing specifications, etc.
npx skill4agent add doccker/cc-use-exp go-devReference Sources: Effective Go, Go Code Review Comments, uber-go/guide
goimports -w . # Format and organize imports
go vet ./... # Static analysis
golangci-lint run # Comprehensive check
go test -v -race -cover ./... # Testing (including race detection and coverage)| Type | Rule | Example |
|---|---|---|
| Package Name | Lowercase words, no underscores | |
| Variable/Function | CamelCase, consistent case for abbreviations | |
| Constant | Exported ones use CamelCase, private ones can use CamelCase or all uppercase | |
| Interface | Single-method interfaces use method name + er | |
commonutilbaseimport (
"context" // Standard library
"fmt"
"github.com/gin-gonic/gin" // Third-party libraries
"project/internal/model" // Internal project code
)// ✅ Good: Add context
if err != nil {
return fmt.Errorf("failed to query user %d: %w", userID, err)
}
// ❌ Bad: Ignore error
result, _ := doSomething()%werrors.Is()errors.As()context.Context// ✅ Good: Control with context
func process(ctx context.Context) error {
done := make(chan error, 1)
go func() { done <- doWork() }()
select {
case err := <-done:
return err
case <-ctx.Done():
return ctx.Err()
}
}go test -race// Table-driven test
func TestAdd(t *testing.T) {
tests := []struct {
name string
a, b int
expected int
}{
{"positive", 1, 2, 3},
{"zero", 0, 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Add(tt.a, tt.b)
if got != tt.expected {
t.Errorf("Add(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.expected)
}
})
}
}| Pitfall | Solution |
|---|---|
| String concatenation in loops | Use |
| Unpreallocated slice | |
| N+1 queries | Batch queries + preloading |
| Unbounded concurrency | Use semaphore or worker pool |
# Performance analysis
go test -cpuprofile=cpu.prof -bench=.
go tool pprof cpu.profproject/
├── cmd/ # Executable entry points
├── internal/ # Private code
│ ├── handler/
│ ├── service/
│ ├── repository/
│ └── model/
├── pkg/ # Public code
├── go.mod
└── go.sumreferences/go-style.md📋 This response follows:- [Specific Section]go-dev