“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

primers

“Kovogo, why can’t I submit the code?”

Golangci-lint sent a submission back because a line of code was too long and exceeded the limit. Lint sent a submission back because a line of code was too long and exceeded the limit.

So how does checking code specifications before submitting code work? Which brings us back to Githook.

githook

What is a githook?

Generally speaking, software/middleware/open source frameworks that are scaled up will more or less provide hooks for developers to use. The purpose of these hooks is to introduce developers into these frameworks/software to write code that changes the presentation or behavior logic of the software.

Githook is the function that Git provides to developers to perform important operations in Git to import developers’ code. For example, the function of introducing code checks at commit time mentioned above is implemented through Githook.

When each Git repository is initialized, an hooks directory is created in the.git directory that contains developer-defined hook scripts.

When you install Git on Windows, git bash comes with you, so you don’t need to worry about script compatibility

When the hook script returns 0, it indicates that the corresponding operation is allowed, and when the hook script returns a non-0 value, it indicates that the operation is rejected.

Each program exits with a return value, which is usually used to indicate whether the program executed successfully.

In the Linux shell we can do this by saying, $? To get the return value of the previous program.

As shown above, we try to access a path that does not exist, and the return value of the ls command is 2.

You can see the following files in.git/hooks:

The.sample file is a reference file provided by Git. If the suffix “.sample “is removed, the hook name will be used

If we need to test our code when we commit it, we need to write a code specification test script for the pre-commit hook.

More details can be found in the official documentation

golintci-lint

Since Golint is no longer maintained by Google, we used Golangci-Lint to check the code specification.

The installation

There are two ways to install Lint on golangci-Lint:

  • In binary installation, directly download the executable file of the corresponding platform
  • If the version is less than1.16usego getDownload the source file and compile the installation, greater than1.16Use thego install
#Go 1.16 +Go install github.com/golangci/golangci-lint/cmd/[email protected]
# Go version < 1.16Go get the -u github.com/golangci/golangci-lint/cmd/[email protected]Copy the code

On Windows, for example, my Golang version is 1.16.10

So we use go Install to install it

use

The code in the target directory can be detected using the run command

Golangci -lint run [directory]Copy the code

You can use -h to view more help information about the run command

golangci-lint run -h
Copy the code

Notable options are:

  • --issues-exit-codeError code returned by code detection failure. Default is1
  • -c, --config PATHConfiguration files for customizing code inspection projects (yamlFormat)
  • --skip-filesFiles to skip
  • --skip-dirsThe directory to skip
  • --enableThe code detection item to enable

Custom code detection

As mentioned above, we can customize the configuration of the code inspection project with –config in the following format:

linters-settings:
  cyclop: The code detection project name usually corresponds to a plug-in
    # Maximum complexity
    max-complexity: 10
    # Maximum complexity allowed per package
    package-average: 0.0
    Whether to skip the test file
    skip-tests: false
Copy the code

See the official documentation for more information

githook + golangci-lint

For Windows, let’s create a new project

Then create a new code file and write some random code:

func main(a) {
	for i := 0; i < 1024; i++ {
		for j := 0; j < 1024; j++ {
			for n := 0; n < 1024; n++ {
				fmt.Printf("%d-%d-%s", i, j, n)
			}
		}
	}
}
Copy the code

Golangci-lint detects that Printf passed an incorrect argument

Let’s add some “bad taste” code to the project, as shown below :(example from cyclop project test file)

func T(a) {
	i := 1
	if i > 2 {
		if i > 2{}if i > 2{}if i > 2{}if i > 2{}}else {
		if i > 2{}if i > 2{}if i > 2{}if i > 2{}}if i > 2{}}Copy the code

Check the code again, noting that you need to enable the cyclomatic complexity detection plug-in cyclop

If we want our code to pass detection, we need to use the custom code detection project mentioned above.

Create lint.yaml in your project and write the configuration items as follows

linters-settings:
  cyclop:
    # Maximum complexity
    max-complexity: 15
    # Maximum complexity allowed per package
    package-average: 0.0
    Whether to skip the test file
    skip-tests: false
Copy the code

Run again and specify the configuration file. The cyclomatic complexity prompt is no longer displayed

pre-commit hook

Using the knowledge above, the logic of our pre-commit hook script looks like this:

  • Checks whether golangci-Lint is installed and installs it if not
  • Run golangci-lint to test the code, returning 1 on failure or 0 on success
#! /bin/shecho "Start lint code" if ! (golangci-lint.exe --version); Then go install github.com/golangci/golangci-lint/cmd/[email protected] fi#Directory name followed by... Indicates a recursive lookup of the directoryif ! (golangci-lint.exe run --enable cyclop --config ./lint.yaml ./...) ; then echo "Lint fail!" exit 1 fi echo "Lint success" exit 0Copy the code

Try submitting the code, and the result will look like the following figure