
golang-standards
Golang 开发规范检查:遵循 Go 官方惯用法和最佳实践,包括代码审查、静态分析和格式检查
- version
- 1.0.0
Install
/plugin install golang-standards@260112-cc-plugins-studyRun it inside Claude Code after adding the marketplace with /plugin marketplace add lwmacct/260112-cc-plugins-study.
Contents
Discovered from sourceCommands 3
Check and suggest formatting improvements for Go code according to Go conventions:
Formatting Checks:
-
Standard Formatting
- Run
gofmt -sto 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)
- Run
-
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
-ersuffix (e.g.,Reader,Writer) - Test functions:
Test<FunctionName>
- Package names: lowercase, short, descriptive
-
Struct Field Ordering
- Exported fields before unexported
- Group related fields together
- Consider cache line alignment for performance-critical structs
-
Function Organization
- Keep main logic early in file
- Helper functions after main functions
- Tests in separate
_test.gofile
-
Import Ordering
- Standard library
- Third-party packages
- Local/internal packages
- Separate with blank lines
- Remove unused imports
-
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
- Use
-
Common Patterns
- Use
varkeyword sparingly (let type inference work) - Prefer
:=overvarfor local variables - Use raw string literals
`for regex and multi-line strings
- Use
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:
-
Unused Code
- Unused variables, functions, or imports
- Unused struct fields or method receivers
-
Error Handling Issues
- Unchecked errors (must handle all errors)
- Errors assigned but not checked
-
Struct Tag Issues
- Duplicate or malformed struct tags
- Standard tags like
json,yaml,dbshould be correct
-
Comment Quality
- Exported functions must have documentation comments
- Comments should be complete sentences
- Package comments should be present in doc.go
-
Formatting
- Code must pass
gofmt - Indentation should use tabs, not spaces
- Line length typically under 100-120 characters
- Code must pass
-
Common Anti-patterns
- Using
time.Sleepinstead of proper synchronization - Empty
selectstatements - Deferring in loops without wrapping in anonymous function
- Comparing errors with
nilincorrectly
- Using
-
Concurrency Issues
- Data races in goroutines
- Missing mutex protection for shared data
- Channel operations that may block indefinitely
-
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:
-
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.Errorfwithout "%w" verb
-
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.,
HTTPServernotHttpServer)
-
Goroutines and Concurrency
- Goroutines should have clear lifecycle management
- Use
context.Contextfor cancellation and timeouts - Avoid goroutine leaks (always handle
Donechannel or usesync.WaitGroup) - Use channels for communication, don't share memory by communicating
-
Interface Design
- Interfaces should be small (ideally 1-2 methods)
- Accept interfaces, return structs
- Define interfaces where they are used, not in advance
-
Code Structure
- Keep functions short and focused
- Avoid deep nesting (prefer early returns)
- Use
go fmtstandard formatting - Group related functions, organize by responsibility
-
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)
-
Testing
- Table-driven tests for multiple cases
- Use
t.Parallel()for independent tests - Test file should be
_test.goin same package
-
Package Organization
- Avoid circular dependencies
- Keep internal packages truly internal
- Use
go modfor 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