Pre-commit hooks with Go

Posted on 29 August 2021 - 1 min read

Git hook scripts are becoming more and more popular. We run our hooks on every commit to automatically point out some simple issues in code before submission to code review. By pointing out these issues in this early phase, it allows a faster feedback loop than waiting for CI. In Javascript world, it's husky that makes Git hooks easy. While in Go, Git hooks are still uncommon. In this post, we'll go through steps to use pre-commit to set up Git hook for a Go project.

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:

  - repo:
    rev: v3.2.0
      - 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:
  rev: v1.41.1
    - 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 install

Now 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.

Tagged with: go, golang, pre-commit, hooks, git, git-hook

Previous: Dependency injection with Go

Next: Some thoughts on writing tests

Bong Nguyen © 2016 - 2022