Warning
Please check Buf Usage Guide for notes on proto styles.
This style guide provides conventions and best practices for writing clean, maintainable, and consistent code in this Go project. Following this guide ensures that the codebase remains easy to read and contributes to a collaborative environment.
- Use idiomatic Go: Follow the official Go code review comments.
- Write small, focused functions: Keep functions short and focused on a single task.
- Use meaningful names: Variables, functions, and package names should clearly describe their purpose.
- Avoid commented-out code: Remove unused code instead of commenting it out.
- Add comments for exported identifiers: Use
//comments for all exported types, functions, and methods.
- Use
gofumpt -extrafor formatting. - Install with:
go install mvdan.cc/gofumpt@latest
- Indentation: Use tabs for indentation.
- Imports:
- Group imports into three sections:
import ( "standard/library" "external/library" "project/internal/package" )
- Use
goimports -local github.com/cresplanex/bloader -w .to organize imports automatically.
- Group imports into three sections:
- Package names should be short and meaningful (e.g.,
utils,auth). - Avoid using underscores or camel case in package names.
- Use camelCase for local variables and function arguments.
- Use meaningful, descriptive names for variables.
- Short variable names like
i,j, orxare acceptable in small scopes (e.g., loops).
- Use
ALL_CAPSfor unexported constants (e.g.,defaultTimeout). - Use camelCase for exported constants (e.g.,
MaxRetries).
- Use camelCase for function and method names.
- Prefix test helper functions with
test(e.g.,testValidateInput).
- Use PascalCase for exported structs and interfaces.
- Use meaningful names that describe their purpose (e.g.,
User,AuthService).
- Write table-driven tests for functions with multiple input-output scenarios.
- Use meaningful test names, starting with
Test(e.g.,TestCalculateSum). - Mock dependencies where necessary to isolate functionality.
- Check for edge cases and invalid inputs.
- Use
gotestsumto run tests:gotestsum --format=short-verbose
- Follow the standard Go project layout:
/cmd # CLI commands /internal # Private application code
- Global variables: Minimize their use; prefer dependency injection.
- Error handling:
- Always check for errors.
- Use meaningful error messages.
- Wrap errors with context using
fmt.Errorforerrors.Join.
- Panic: Avoid using
panicfor error handling unless absolutely necessary.
- Set up your editor to automatically format code on save.
- We support
vscodewithtaskandMakefile. For more information, check the Automation Guide. - Run
golangci-lintto catch linting issues.golangci-lint run
- Include pre-commit hooks to ensure formatting and linting:
gofumpt -extra -w . && \ find . -name "*.go" -not -path "./gen/*" -exec goimports -w -local github.com/cresplanex/bloader {} + && \ golangci-lint run
Adhering to this guide will ensure a consistent, maintainable, and high-quality codebase. Happy coding! 🎉