rasooll/golang-rules icon
public
Published on 4/2/2025
rasooll/golang-rules

Rules

Code Quality Rules

  • Follow Go's official style guide and idioms from "Effective Go"
  • Use proper error handling with explicit checks (if err != nil)
  • Prefer composition over inheritance
  • Write concise, descriptive function and variable names
  • Include appropriate comments for complex logic and exported functions/types
  • Use proper package organization and naming
  • Implement proper resource cleanup (defer, close, etc.)
  • Apply consistent formatting (use gofmt standards)
  • Favor readability over excessive optimization
  • Use appropriate Go concurrency patterns (goroutines, channels, sync package)

Architecture and Design Rules

  • Follow SOLID principles where applicable
  • Use interfaces for flexible and testable code
  • Separate concerns (business logic, data access, presentation)
  • Design for testability
  • Create small, focused packages with clear responsibilities
  • Use dependency injection for better testing and flexibility
  • Implement proper error types and handling strategies
  • Design APIs with consistency and backward compatibility in mind
  • Apply appropriate design patterns but avoid over-engineering
  • Consider performance implications of design choices

Testing Rules

  • Write comprehensive unit tests for all functionality
  • Use table-driven tests for clear and concise test cases
  • Implement integration tests for critical paths
  • Use appropriate mocks and test doubles
  • Test error conditions and edge cases
  • Benchmark performance-critical code
  • Use Go's built-in testing tools (testing package, benchmarks)
  • Practice test-driven development when appropriate
  • Keep tests maintainable and understandable
  • Ensure adequate test coverage

Performance and Optimization Rules

  • Use appropriate data structures and algorithms
  • Profile before optimizing (pprof)
  • Optimize for memory efficiency in large-scale systems
  • Consider CPU and I/O bound operations
  • Use buffered operations for I/O intensive tasks
  • Apply concurrency with caution and purpose
  • Design for scalability from the beginning
  • Minimize allocations in performance-critical paths
  • Use sync.Pool for frequent allocations
  • Consider cache-friendly data structures

Security Rules

  • Validate all user inputs
  • Sanitize data before using in SQL, templates, etc.
  • Use secure cryptographic practices
  • Implement proper authentication and authorization
  • Handle sensitive data carefully (passwords, tokens)
  • Use the principle of least privilege
  • Check for common security vulnerabilities (OWASP)
  • Consider rate limiting for public APIs
  • Implement proper logging (without sensitive data)
  • Keep dependencies updated and scan for vulnerabilities

Documentation Rules

  • Document all exported functions, types, and constants
  • Provide clear package documentation
  • Include examples in documentation when helpful
  • Document concurrency considerations
  • Explain non-obvious design choices
  • Document known limitations and edge cases
  • Include usage examples for complex APIs
  • Keep documentation up-to-date with code changes
  • Document error conditions and handling
  • Use godoc compatible documentation style