1. golangci-lint

1.1 Environment Setup

  1. Install golangci-lint

    1$ go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest  
    

    Example batch script for linting all Go modules in a project:

     1@if "%DEBUG%"=="" @echo off  
     2@rem ##########################################################################  
     3@rem  
     4@rem Lint all Go modules in backend and frontend folders  
     5@rem  
     6@rem ##########################################################################  
     7
     8@rem Set local scope for variables in Windows NT shell  
     9if "%OS%"=="Windows_NT" setlocal  
    10
    11setlocal enabledelayedexpansion  
    12
    13@REM Get the directory of this script  
    14set BAT_PATH=%~dp0  
    15@REM Navigate to the code directory to ensure consistent behavior  
    16cd /D %BAT_PATH%  
    17cd ..  
    18
    19@rem Get the absolute path of the current directory  
    20for %%A in ("%cd%") do set "CURRENT_DIR=%%~fA"  
    21
    22@REM Iterate through all go.mod files  
    23for /r "%CURRENT_DIR%" %%A in (go.mod) do (  
    24set "MOD_FILE=%%A"  
    25set "SUB_DIR=%%~dpA"  
    26pushd "!SUB_DIR!"  
    27
    28if exist "go.mod" (  
    29    echo !SUB_DIR!  
    30    @REM Run linting tools  
    31    golangci-lint -c ../../.golangci.yml run --timeout=10m  
    32    goimports-reviser -company-prefixes "agilebot.com.cn/app/mts" ./...  
    33)  
    34popd  
    35)  
    36
    37pause  
    

1.2 Common Commands

  1. Basic commands:

    1$ golangci-lint version  
    2$ golangci-lint cache status  # Check cache status  
    3$ golangci-lint cache clean   # Clear cache  
    
  2. Skipping specific linters:
    Add //nolint:linter1,linter2,... above the line to skip checks.
    Example: //nolint:golint,errcheck skips checks for golint and errcheck. Always add a comment explaining why the check is skipped.

1.3 Common Issues

  1. Unhandled Errors: Methods returning error must be checked, not ignored.

  2. Naming Conventions: Use CamelCase for variables. Constants can use CamelCase or ALL_CAPS.
    Example configuration in .golangci.yml:

     1revive:  
     2    severity: error  
     3    rules:  
     4      - name: error-naming  
     5      - name: error-return  
     6      - name: errorf  
     7      - name: import-shadowing  
     8      - name: var-naming  
     9        arguments:  
    10          - ["ID", "IDS", "IP", "URL", "HTTP", "JSON", "XML"]  # Allowed uppercase abbreviations  
    11          - []  
    12          - - upperCaseConst: true  
    13              skipPackageNameChecks: true  
    

    Ensure the .golangci.yml format is correct. The revive linter supports upperCaseConst in newer versions (see GitHub discussion).

  3. Error Handling: Prefer github.com/pkg/errors over fmt for error formatting.

  4. Memory Pre-allocation: Optimize slice/map initializations.

  5. Unused Assignments: Remove redundant variable assignments.

  6. Name Conflicts: Avoid variable names that clash with imported packages.

  7. Named Return Values: Discouraged unless necessary.

  8. Unnecessary Type Conversions: Simplify code where possible.

  9. Other issues: Refer to the linter documentation for details.

2. goimports-reviser

A tool for organizing imports into groups and formatting code automatically. Ideal for code cleanliness enthusiasts!

Installation:

1$ go install -v github.com/incu6us/goimports-reviser/v3@latest