A simple configurable Go code linter that validates code naming conventions for folders, files, functions, variables, constants, structs, and interfaces. It also supports pre-commit hook integration to prevent bad naming conventions from being committed.
In principle, code validation should occur during the commit phase rather than at push time. This approach ensures that any issues can be addressed immediately, eliminating the need for a soft reset or force push.
/go-linter-tool
├── main.go # CLI entry point (init/lint)
├── internal/
│ ├── check.go # AST naming rule checks (functions, vars, consts, types)
│ ├── config.go # Default configuration and JSON loading
│ ├── init.go # Pre-commit hook initializer
│ └── linter.go # Core linter logic and file walking
├── .go.linter.json # Configuration file (auto-generated)
├── pre-commit-hook.sh # Git hook script
└── README.md # Project documentation
- Validates naming rules:
- Folder and file names
- Function and handler names
- Variable and constant names
- Struct and interface names
- Uses JSON config:
.go.linter.json - Skips ignored folders/files
- Git pre-commit hook integration
go install github.com/yaza-putu/golinter@latestif it's not accessible, try moving it to the global bin
mv ~/go/bin/golinter /usr/local/binIn your project folder, run the command below:
$ golinter initThis will:
- Create
.go.linter.jsonif missing - Copy
pre-commit-hook.shinto.git/hooks/pre-commit
git commit -m "message"or
golinter lint .- Main CLI entry point
- Handles commands:
init: generates config + sets hooklint <path>: runs linting logic
- Defines
Configstruct - Loads config from
.go.linter.json - Supplies defaults if config not found
- Handles setup of Git pre-commit hook
- Makes the script executable on UNIX-based systems
- Walks through project folder
- Applies folder & file naming checks
- Parses
.gofiles into ASTs
- Applies AST-level rules
- Naming for: functions, variables, constants, interfaces, structs
- JSON config file for all lint rules
- Customizable naming regex, descriptions, and exceptions
- Executed automatically by Git before commit
- Runs
go run main.go lint . - Blocks commit if lint errors exist
[ERROR] my_handler.go:5:1 - Handler function 'my_handler' doesn't match pattern: Handler functions should be PascalCase and end with 'Handler'
Suggestion: Use PascalCase and end with 'Handler'
Edit .go.linter.json to change:
- Regex patterns
- Description messages
- Naming exceptions
Once init is run, every git commit will trigger the linter. If violations exist, the commit will be blocked.
MIT