|
| 1 | +--- |
| 2 | +description: Best practices for writing Go code |
| 3 | +globs: *.go |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | + |
| 7 | +# Go Best Practices |
| 8 | + |
| 9 | +## Code Style and Structure |
| 10 | + |
| 11 | +1. Follow Go best practices and standard formatting: |
| 12 | + - Use `gofmt` for basic code formatting |
| 13 | + - Use `goimports` for import organization |
| 14 | + - Use `golangci-lint` for comprehensive linting |
| 15 | + - Follow Go naming conventions |
| 16 | + - Keep functions focused and small |
| 17 | + - Use meaningful variable and function names |
| 18 | + - Prefer using Google's and Uber's style guide when authoring Go code |
| 19 | + - Prefer using internally available packages over external packages or standard library packages |
| 20 | + - Most "helper" libraries are found under `internal/core/common` or `internal/adapter` |
| 21 | + |
| 22 | +2. Component Development: |
| 23 | + - Place scalar components in `crib/scalar/[component]/v1/` |
| 24 | + - Place composite components in `crib/composite/` |
| 25 | + - Each component should have clear props and validation |
| 26 | + - Include proper documentation and examples |
| 27 | + |
| 28 | +3. High-level Directory Structure: |
| 29 | + - GitHub Actions workflows in `.github/workflows` |
| 30 | + - API schemas like OpenAPI in `api` |
| 31 | + - Build files like Dockerfile in `build` |
| 32 | + - CLI-related files in `cmd` |
| 33 | + - SDK "Plans" in `contrib` |
| 34 | + - Components in `crib/scalar` and `crib/composite` |
| 35 | + - Core SDK exported methods in `crib` |
| 36 | + - Deployment automation like Docker Compose in `deployments` |
| 37 | + - Examples in `examples` |
| 38 | + - Hack scripts in `hack` |
| 39 | + - Adapters (Hexagonal Architecture) in `internal/adapter` |
| 40 | + - Common utilities in `internal/core/common` |
| 41 | + - Domain objects in `internal/core/domain` |
| 42 | + - Interfaces in `internal/core/interfaces` |
| 43 | + - Services in `internal/core/services` |
| 44 | + - Taskfiles in `taskfiles` |
| 45 | + - E2E tests in `tests` |
| 46 | + |
| 47 | +## Documentation |
| 48 | + |
| 49 | +1. Code Documentation: |
| 50 | + - Document all exported functions and types |
| 51 | + - Include usage examples in comments |
| 52 | + - Keep README.md up to date |
| 53 | + - Document any breaking changes |
| 54 | + |
| 55 | +2. API Documentation: |
| 56 | + - Document all public APIs |
| 57 | + - Include parameter descriptions |
| 58 | + - Provide usage examples |
| 59 | + - Document error cases |
| 60 | + |
| 61 | +## Development Workflow |
| 62 | + |
| 63 | +1. Pre-commit Checks: |
| 64 | + - Run `task go:lint` before committing |
| 65 | + - Run `task go:test` to ensure tests pass |
| 66 | + - Run `task go:fmt` to format code |
| 67 | + - Check for any security vulnerabilities |
| 68 | + |
| 69 | +2. Pull Request Process: |
| 70 | + - Update documentation if needed |
| 71 | + - Add tests for new features |
| 72 | + - Ensure CI passes |
| 73 | + - Get at least one review |
| 74 | + |
| 75 | +## Component Guidelines |
| 76 | + |
| 77 | +1. Scalar Components: |
| 78 | + - Keep components simple and focused |
| 79 | + - Implement proper validation |
| 80 | + - Handle errors gracefully |
| 81 | + - Include proper logging |
| 82 | + |
| 83 | +2. Composite Components: |
| 84 | + - Document dependencies clearly |
| 85 | + - Handle component ordering |
| 86 | + - Include proper error handling |
| 87 | + - Provide clear configuration options |
| 88 | + |
| 89 | +## Security Guidelines |
| 90 | + |
| 91 | +1. General Security: |
| 92 | + - Never hardcode sensitive information |
| 93 | + - Use secure defaults |
| 94 | + - Validate all inputs |
| 95 | + - Follow principle of least privilege |
| 96 | + |
| 97 | +## Performance Guidelines |
| 98 | + |
| 99 | +1. Resource Usage: |
| 100 | + - Optimize memory usage |
| 101 | + - Minimize API calls |
| 102 | + - Use appropriate caching |
| 103 | + - Monitor resource consumption |
| 104 | + |
| 105 | +2. Scalability: |
| 106 | + - Design for horizontal scaling |
| 107 | + - Use efficient data structures |
| 108 | + - Implement proper error handling |
| 109 | + - Consider edge cases |
| 110 | + |
| 111 | +## Error Handling |
| 112 | + |
| 113 | +1. Error Management: |
| 114 | + - Use proper error wrapping |
| 115 | + - Include context in errors |
| 116 | + - Handle all error cases |
| 117 | + - Provide clear error messages |
| 118 | + |
| 119 | +2. Logging: |
| 120 | + - Use appropriate log levels |
| 121 | + - Include relevant context |
| 122 | + - Follow logging best practices |
| 123 | + - Consider log rotation |
0 commit comments