Pre-commit hooks with Go
Posted on 29 August 2021 - 1 min read
The installation's pretty easy by following their document. I have a Macbook so I'll use
brew to install it:
brew install pre-commit
Next, we'll add pre-commit configuration by creating a file named
.pre-commit-config.yaml. You can either create the file from scratch or from a provided sample like:
pre-commit sample-config > .pre-commit-config.yaml
The configuration file will look like:
repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v3.2.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files
Then I'll add the following lines in order to run
golangci-lint before every commit that has Go files:
- repo: https://github.com/golangci/golangci-lint rev: v1.41.1 hooks: - id: golangci-lint
For the full set of options, please check their document.
After that, run the below command to set up the git scripts:
pre-commit will run automatically on
git commit. You can also use
pre-commit run -a to run the git hook against all files.
Yeap, that's all about it. For reference, you can find an example of using
pre-commit in a Go project in this repo.