Golang: Improving your GO project With pre-commit hooks

--

This article I will share about how my team deal with making code alignment, convention and adopt the pre-commit to go project.

“ Code Convention + Alignment + Team Rules ”

The quote above is not a new things to do, but I found several projects ignore it or forget it. Many time we delegate this task to be pull request, and pull requester feel

Real-Life Usecase

I would like to share from my previous large scale project, with many contributors works on the same repository. To make the coding alignment, and convention. The beginning of project we trying to project setup adopted the tools like linter, code formatter, and customize script hook on save for Goland or VSCode.

I found the problem is that it pretty hard to make everyone do the same rules on their editor if your team have big sizing of software developer.

Above is the routine jobs when I and my team need to commit the code to remote repository. You will see those development pipeline items it’s pretty straightforward.

[your go code]
|_ gofmt
|_ goimports
|_ go vet
|_ golangci-lint
|_ go test
|_ any others go tools ... // or your hooks

If you are the nodejs developer you probably familiar with husky to trigger your hooks. But for golang developer 2018, I did not seen any suitable tools for my team.

Until we met the pre-commit, my world was changed from dark clouded to sunrise 🌞.

Alright guys, when you read til here. Would it be better if you could change it? to be performed automatically workflow.

Pre-Commit Hooks

This action so pretty powerful to make sure that the way of improving your software development especially ensure delivery a good code quality.

performing tasks within the project repository that can be automated should be a great way to help the team review code faster since the developer filtered out non-standard code and refactored code to be team standard before sending the pull request.

Commitlint

Commitlint is the conventional commits message for adding human and machine readable meaning to commit message.

In generally, add linter tothe commit message will help team to know exactly the context of what you need to commit the code.

common prefix of the commit message type can be:

  • build
  • chore
  • ci
  • docs
  • feat
  • fix
  • perf
  • refactor
  • revert
  • style
  • test

Example

fix(server): middleware send CORS header

Goals

  1. Setup basic hooks
  2. Setup golang hooks
  3. Setup commitlint hook

Ready? Let’s get started

Photo by Hitesh Choudhary on Unsplash

we will go through the step to install pre-commit and also adding hooks step-by-step to go project.

Before you can run hooks, you need to have the install pre-commit to your development machine. In this article I am going to install viabrew tool.

brew install pre-commit

pre-commit configuration

Th next one is to create a new file named .pre-commit-config.yaml to your root project. If you are not familiar with this tool I suggest you can reach full documentation here.

Add pre-commit-hooks

The next step is to add essential basic project checker hooks to pre-commit file as below:

  • trailing-whitespace to handle any whitespace of the end of line and the new line.
  • end-of-file-fixer to remove EOF of your whole files project.
  • check-yaml to fix yaml format file.
  • check-added-large-files this job will let you to know which file has large file size.

Well, now we have basic project checker whole files under the project.

Add Golang hooks

  • go-fmt trigger automatically formats Go source code of your whole project.
  • go-imports automatically update your Go import lines (add missing and remove unreferenced imports).
  • no-go-testing finding and warning which files does not have test cover.
  • golangci-lint run linter.
  • go-unit-tests run go test command.

Golang CI Lint You can create your linter rules to .golangcilint.yaml and linter you can discover more https://golangci-lint.run

now your file .pre-commit-config.yaml will look like:

Actually, you can write your own hook to make sure that all committed code has apply your team standard and keep conventional.

The next step is to hook the commitlint.

Install commitlint

npm install -g @commitlint/cli @commitlint/config-conventional

Create commitlint.config.js to your root project

echo “module.exports = {extends: [‘@commitlint/config-conventional’]};” > commitlint.config.js

For full file.pre-commit-config.yaml with all hooks config will be:

Lastly, you will run pre-commit install command to set up the git hook scripts.

Almost done setting up the project, you can trigger hook by commit you code through git commit command.

Example passed pre-commit hooks
Example failed pre-commit hooks

Conclusion

so in this blog, we can improve your go project workflow by using pre-commit to consolidate existing tools such as go fmt to ensure that whatever you commit up to your repos has been properly formatted.

Linter to make sure your go code have good quality, and also commitlint to help team to know exactly what you did changes.

I suggest you make your own hook and hosted it in your place because the hook may be changed by the origin author.

I’ve push all code and project an example as below.

--

--

Teerapong Singthong 👨🏻‍💻
Teerapong Singthong 👨🏻‍💻

Written by Teerapong Singthong 👨🏻‍💻

Engineering Manager, ex-Solution Engineering Lead at LINE | Tech | Team Building | System Design | Architecture | SWE | Large Scaling System

Responses (3)