Home  >  Article  >  Backend Development  >  Solution to use golangci-lint locally

Solution to use golangci-lint locally

Golang菜鸟
Golang菜鸟forward
2023-08-04 17:02:291148browse

Why do we need code inspection?

Generally, code inspection is important for three reasons:

  1. Avoid low-level bugs: some common code problems, if they cannot be discovered in time before compilation or running, Syntax problems in the code will directly lead to compilation or runtime errors, affecting development efficiency and code quality;

  2. Unified coding habits: Every team or individual will have it Some code specifications or coding habits, in order to facilitate later maintenance and reading, the code we write also needs to comply with certain format specifications;

  3. Ensure the quality of online code: In version management, we need to automatically perform some code checking work before submitting or publishing to ensure that our code meets the final version requirements.

golangci-lint As golang supports the best code inspection tool, and it is widely used in the golang open source library, this can be explained. For better development of the project and better maintenance in the later period, we can also try to introduce it.

Problems when trying to use goalngci-lint

In our daily development process, our customary behavior is: code -> git add -> git commit - > git push one-stop service.

1. Manual execution mode:

Then at this time we need to check whether our code has been completed before git commit If it meets certain rules, you need to manually execute golangci-lint run ./..., or perform code inspection before git push. Since our commit and push are daily operations, they are likely to be frequent, and this manual method is somewhat cumbersome. In the process of using it, due to some things, you may forget it, or you may not execute it directly, feeling troublesome and having full confidence in your own code. So after using it as a whole, it is still very emm. You should know that it is not smooth.

2. gitlab runner mode:

The reason why I thought of this solution may be that I saw too many open source libraries using servers Code inspection (although it is only PR)

I built a gitlab through docker and configured the runner through the tutorial. This took me 2 days, but I needed to learn the ci configuration of gitlab and runner. job configuration, and the installation of some tools, but the results may not be satisfactory.

Thinking that the results of the server check are on gitlab, our development basically (the first time we pull the code) will not go to gitlab, so how can this error message be delivered to the corresponding person? Or if multiple people write a function, how to allocate errors? This solution using server check took 2 days to pass.

3. Local pre-commit

Since the server check does not work, I will check the script before submitting the code. If there are errors, By not allowing commits, this forces you to deal with your errors. Hahaha (the mechanism) will not tell you that you can force submission (use it with caution).

This begins the writing of the local pre-commit script. I searched for information on the Internet and wrote a pre-commit script based on my own needs. Mainly codefmt and codegolangci-lint are checked. I thought I would definitely make it this time!

At this time, the script is written, but how to upload it to the server? Since the .git folder cannot be uploaded to the server by default, we can only write an init.sh script to upload it to the server. All developers When you get the project for the first time, you only need to execute it and configure it locally, which is much simpler.

Try running:

Solution to use golangci-lint locally

. . . The result doesn't work. The words here cannot express my feelings.

After a series of operations, it was found that golangci-lint does not support checking of single files.

The principle of my script checking is as follows:

  1. Filter all go files modified in the current commit

  2. Run each file oncegolangci-lint run xxx.go

This It's embarrassing; I originally wanted to use a script to filter out the folders where all the files are located, and only run it on the folders, because writing this script is a bit complicated for me (not that difficult). Of course, another problem is that the execution process of golangci-lint takes a long time. We cannot say that we have to wait for 1 minute for each commit. This will increase the fishing time in disguise[/doge].

4. Local pre-commit pre-push

After much tossing, a relatively previous solution was finally born, which is very suitable for us. a method.

Modify the default git hook location through init.sh to the githooks folder we created. For specific operations, you can view the script content. I I think the script is quite clear.

Through pre-commit, we provide fmt and import checks and automatic formatting and then automatically add them to avoid some operations where we forget to format ourselves.

Use pre-push to check whether the project is pushed to the server in a usable state. Since push is not that frequent, the inspection of the entire project is used at this time.

The above is the detailed content of Solution to use golangci-lint locally. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Golang菜鸟. If there is any infringement, please contact admin@php.cn delete