Golang 开发规范检查:遵循 Go 官方惯用法和最佳实践,包括代码审查、静态分析和格式检查

version
1.0.0

Install

/plugin install golang-standards@260112-cc-plugins-study

Run it inside Claude Code after adding the marketplace with /plugin marketplace add lwmacct/260112-cc-plugins-study.

Contents

Discovered from source

Commands 3

Check and suggest formatting improvements for Go code according to Go conventions:

Formatting Checks:

  1. Standard Formatting

    • Run gofmt -s to simplify code
    • Use tabs for indentation (not spaces)
    • No trailing whitespace
    • One blank line between top-level declarations
    • Opening braces on same line (not new line)
  2. Naming Conventions

    • Package names: lowercase, short, descriptive
      • ✅ httpserver, user, auth
      • ❌ httpServer, myPackage, v1
    • Constants: PascalCase or UPPER_SNAKE_CASE
    • Variables: camelCase for local, PascalCase for exported
    • Interfaces: typically -er suffix (e.g., Reader, Writer)
    • Test functions: Test<FunctionName>
  3. Struct Field Ordering

    • Exported fields before unexported
    • Group related fields together
    • Consider cache line alignment for performance-critical structs
  4. Function Organization

    • Keep main logic early in file
    • Helper functions after main functions
    • Tests in separate _test.go file
  5. Import Ordering

    • Standard library
    • Third-party packages
    • Local/internal packages
    • Separate with blank lines
    • Remove unused imports
  6. Comment Style

    • Use // for all comments (not /* */)
    • Doc comments start with the name being documented
    • No blank line between comment and declaration
    • Package comments at top of file
  7. Common Patterns

    • Use var keyword sparingly (let type inference work)
    • Prefer := over var for local variables
    • Use raw string literals ` for regex and multi-line strings

Example Fixes:

// ❌ Before
func (self *MyStruct) DoThing() {
    var x int = 5
    return x
}

// ✅ After
func (m *MyStruct) doThing() int {
    x := 5
    return x
}

Provide specific formatting suggestions with before/after examples.

Check the Go code for common linting issues and style problems:

Static Analysis Checks:

  1. Unused Code

    • Unused variables, functions, or imports
    • Unused struct fields or method receivers
  2. Error Handling Issues

    • Unchecked errors (must handle all errors)
    • Errors assigned but not checked
  3. Struct Tag Issues

    • Duplicate or malformed struct tags
    • Standard tags like json, yaml, db should be correct
  4. Comment Quality

    • Exported functions must have documentation comments
    • Comments should be complete sentences
    • Package comments should be present in doc.go
  5. Formatting

    • Code must pass gofmt
    • Indentation should use tabs, not spaces
    • Line length typically under 100-120 characters
  6. Common Anti-patterns

    • Using time.Sleep instead of proper synchronization
    • Empty select statements
    • Deferring in loops without wrapping in anonymous function
    • Comparing errors with nil incorrectly
  7. Concurrency Issues

    • Data races in goroutines
    • Missing mutex protection for shared data
    • Channel operations that may block indefinitely
  8. Resource Management

    • File handles not closed (use defer)
    • Goroutines that may leak
    • Connections not properly closed

Output Format:

🔴 SEVERE: [Issue that will cause bugs]
🟡 WARNING: [Style or minor issue]
💡 SUGGESTION: [Improvement opportunity]
   Location: [file:line-range]
   Code: [Relevant snippet]
   Fix: [How to fix]

Prioritize severe issues that affect correctness.

Review the selected Go code according to Go official idioms and best practices:

Check for:

  1. Error Handling

    • All errors must be checked (no bare returns, no ignored errors)
    • Error messages should be descriptive and include context
    • Avoid wrapping errors with fmt.Errorf without "%w" verb
  2. Naming Conventions

    • Package names should be lowercase, single word, short
    • Exported names must use PascalCase (e.g., MyFunction)
    • Unexported names must use camelCase (e.g., myVariable)
    • Acronyms should keep consistent casing (e.g., HTTPServer not HttpServer)
  3. Goroutines and Concurrency

    • Goroutines should have clear lifecycle management
    • Use context.Context for cancellation and timeouts
    • Avoid goroutine leaks (always handle Done channel or use sync.WaitGroup)
    • Use channels for communication, don't share memory by communicating
  4. Interface Design

    • Interfaces should be small (ideally 1-2 methods)
    • Accept interfaces, return structs
    • Define interfaces where they are used, not in advance
  5. Code Structure

    • Keep functions short and focused
    • Avoid deep nesting (prefer early returns)
    • Use go fmt standard formatting
    • Group related functions, organize by responsibility
  6. Pointers and Values

    • Use value receivers for methods that don't modify the receiver
    • Use pointer receivers for methods that modify the receiver
    • Don't use pointers just to avoid copying (Go is efficient)
  7. Testing

    • Table-driven tests for multiple cases
    • Use t.Parallel() for independent tests
    • Test file should be _test.go in same package
  8. Package Organization

    • Avoid circular dependencies
    • Keep internal packages truly internal
    • Use go mod for dependency management

Provide feedback in this format:

❌ ISSUE: [Brief description]
   Location: [file:line]
   Why: [Explanation of Go guideline]
   Fix: [Suggested fix with example]

Focus on critical issues first, then style improvements. Be specific and actionable.

Sponsors

Details

Marketplace
lwmacct/260112-cc-plugins-study
Source
plugins/golang-standards
Author
Claude Code
Category
Development Tools
License
MIT
Keywords
golang, go, standards, code-review, best-practices, linting

More from lwmacct/260112-cc-plugins-study

All 3