|
| 1 | +# Go Development Standards |
| 2 | + |
| 3 | +## Go Version |
| 4 | + |
| 5 | +- **Current version**: Go 1.23 |
| 6 | +- **Update policy**: Keep reasonably current with stable Go releases |
| 7 | + |
| 8 | +## Project Layout |
| 9 | + |
| 10 | +Follow the standard Go project layout: |
| 11 | + |
| 12 | +```txt |
| 13 | +/ |
| 14 | +├── main.go # Application entry point |
| 15 | +├── cmd/ # Command implementations |
| 16 | +├── internal/ # Private application code |
| 17 | +│ └── <package>/ # Internal packages |
| 18 | +├── go.mod # Module definition |
| 19 | +├── go.sum # Dependency checksums |
| 20 | +├── Makefile # Build automation |
| 21 | +└── .golangci.yml # Linter configuration |
| 22 | +``` |
| 23 | + |
| 24 | +## Coding Standards |
| 25 | + |
| 26 | +### Formatting |
| 27 | + |
| 28 | +- Use `gofmt` for all Go files (enforced by pre-commit hook) |
| 29 | +- Use `goimports` for import organization (included in golangci-lint) |
| 30 | + |
| 31 | +### Naming Conventions |
| 32 | + |
| 33 | +- **Packages**: Short, lowercase, single-word names (e.g., `issue`, `cmd`) |
| 34 | +- **Interfaces**: End with `-er` suffix when appropriate (e.g., `Reader`, `Writer`) |
| 35 | +- **Variables**: Use camelCase |
| 36 | +- **Constants**: Use MixedCaps or UPPER_CASE for exported constants |
| 37 | +- **Exported names**: Start with uppercase letter |
| 38 | +- **Unexported names**: Start with lowercase letter |
| 39 | + |
| 40 | +### Error Handling |
| 41 | + |
| 42 | +- Always check errors explicitly |
| 43 | +- Wrap errors with context using `fmt.Errorf` with `%w` verb |
| 44 | +- Return errors up the call stack; handle at the appropriate level |
| 45 | +- Don't use panic for normal error handling |
| 46 | + |
| 47 | +Example: |
| 48 | + |
| 49 | +```go |
| 50 | +content, err := os.ReadFile(filePath) |
| 51 | +if err != nil { |
| 52 | + return fmt.Errorf("failed to read file: %w", err) |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### Testing |
| 57 | + |
| 58 | +- **Location**: Tests go in `*_test.go` files alongside the code |
| 59 | +- **Pattern**: Use table-driven tests for multiple test cases |
| 60 | +- **Coverage**: Run `make coverage` to check coverage |
| 61 | +- **Race detector**: Tests run with `-race` flag in CI |
| 62 | + |
| 63 | +Example table-driven test: |
| 64 | + |
| 65 | +```go |
| 66 | +func TestFunction(t *testing.T) { |
| 67 | + tests := []struct { |
| 68 | + name string |
| 69 | + input string |
| 70 | + want string |
| 71 | + wantErr bool |
| 72 | + }{ |
| 73 | + {"case 1", "input1", "output1", false}, |
| 74 | + {"error case", "bad", "", true}, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + got, err := Function(tt.input) |
| 80 | + if (err != nil) != tt.wantErr { |
| 81 | + t.Errorf("error = %v, wantErr %v", err, tt.wantErr) |
| 82 | + return |
| 83 | + } |
| 84 | + if got != tt.want { |
| 85 | + t.Errorf("got %v, want %v", got, tt.want) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Dependencies |
| 93 | + |
| 94 | +- **Minimize dependencies**: Only add well-maintained, necessary dependencies |
| 95 | +- **Use go.mod**: Manage dependencies with Go modules |
| 96 | +- **Version pinning**: Use specific versions in go.mod |
| 97 | +- **Update regularly**: Keep dependencies updated for security |
| 98 | + |
| 99 | +### Current Dependencies |
| 100 | + |
| 101 | +- `github.com/spf13/cobra` - CLI framework (standard for Go CLI apps) |
| 102 | + |
| 103 | +## Linting |
| 104 | + |
| 105 | +### golangci-lint Configuration |
| 106 | + |
| 107 | +The project uses `golangci-lint` with multiple linters enabled: |
| 108 | + |
| 109 | +- **errcheck**: Unchecked errors |
| 110 | +- **gosimple**: Code simplification |
| 111 | +- **govet**: Suspicious constructs |
| 112 | +- **staticcheck**: Static analysis |
| 113 | +- **gofmt**: Code formatting |
| 114 | +- **goimports**: Import organization |
| 115 | +- **revive**: Comprehensive linting (golint replacement) |
| 116 | +- **gosec**: Security issues |
| 117 | +- **gocyclo**: Cyclomatic complexity (threshold: 15) |
| 118 | +- **gocognit**: Cognitive complexity (threshold: 20) |
| 119 | + |
| 120 | +### Running Linters |
| 121 | + |
| 122 | +```bash |
| 123 | +make lint # Run all linters |
| 124 | +make fmt # Format code |
| 125 | +make vet # Run go vet |
| 126 | +``` |
| 127 | + |
| 128 | +## Build Process |
| 129 | + |
| 130 | +### Local Development |
| 131 | + |
| 132 | +```bash |
| 133 | +make build # Build for current platform |
| 134 | +make test # Run tests |
| 135 | +make coverage # Generate coverage report |
| 136 | +``` |
| 137 | + |
| 138 | +### Multi-Platform Builds |
| 139 | + |
| 140 | +```bash |
| 141 | +make build-all # Build for: |
| 142 | + # - linux/amd64, linux/arm64 |
| 143 | + # - darwin/amd64, darwin/arm64 |
| 144 | + # - windows/amd64, windows/arm64 |
| 145 | +``` |
| 146 | + |
| 147 | +## Performance Considerations |
| 148 | + |
| 149 | +- Use profiling for performance-critical code (`go test -cpuprofile`, `-memprofile`) |
| 150 | +- Pre-allocate slices when size is known |
| 151 | +- Use string builders for string concatenation |
| 152 | +- Be mindful of memory allocations in hot paths |
| 153 | + |
| 154 | +## Security |
| 155 | + |
| 156 | +- Run `gosec` linter (included in golangci-lint) |
| 157 | +- Never hard-code credentials |
| 158 | +- Validate all external inputs |
| 159 | +- Use `crypto/rand` for random numbers (not `math/rand`) |
| 160 | +- Keep dependencies updated for security patches |
| 161 | + |
| 162 | +## Documentation |
| 163 | + |
| 164 | +- **Package comments**: Every package should have a package-level comment |
| 165 | +- **Exported functions**: Document all exported functions, types, and constants |
| 166 | +- **Examples**: Provide examples for complex functionality |
| 167 | +- **README**: Keep README.md updated with usage instructions |
| 168 | + |
| 169 | +## Git Hooks |
| 170 | + |
| 171 | +The pre-commit hook runs: |
| 172 | + |
| 173 | +1. Go formatting check (`gofmt`) |
| 174 | +2. Go vet |
| 175 | +3. golangci-lint |
| 176 | +4. Go tests with race detector |
| 177 | +5. Build verification |
| 178 | + |
| 179 | +All checks must pass before commit. |
0 commit comments