搜索
首页后端开发Golanggolangci-lint应用

golangci-lint应用

Aug 04, 2023 pm 05:03 PM
gogolangci-lint

golangci-lint 是什么?

golangci-lint 是一个 Go linters 聚合器,而 linter 是使用工具来对代码提供一些检查,保证提交代码的质量。golangci-lint 是一个 Go linters 聚合器,而 linter 是使用工具来对代码提供一些检查,保证提交代码的质量。

为什么不直接使用 golangci-lint ?

需要手动执行,在之前使用的过程中,由于项目是多人活动,总是会忘记执行golangci-lint 进行代码检查,当前我自己也是。所以我们希望采用一种隐式的方式来自动执行。那么经过多番思考,采用 git 的 hooks 可以来自动执行一些脚本。

这期间还有一些其他的尝试,不过这个文章主要说 在git中的使用ci-lint,有兴趣的可以移步到为什么最终采用git 本地 hooks 的方式执行golangci-lint?

采用 git 的 hooks 来自动执行检查!

前提条件

请设置 goland 的默认终端为 bash,不然后面执行脚本的时候,可能会不支持。

golangci-lint应用

由于现在采用 git 作为我们的版本管理系统(VCS),而git

为什么不直接使用 golangci-lint ?🎜🎜需要手动执行,在之前使用的过程中,由于项目是多人活动,总是会忘记执行golangci-lint 进行代码检查,当前我自己也是。所以我们希望采用一种隐式的方式来自动执行。那么经过多番思考,采用 git 的 hooks 可以来自动执行一些脚本。🎜🎜这期间还有一些其他的尝试,不过这个文章主要说 在git中的使用ci-lint,有兴趣的可以移步到为什么最终采用git 本地 hooks 的方式执行golangci-lint?🎜🎜🎜🎜采用 git 的 hooks 来自动执行检查!🎜🎜前提条件:🎜🎜请设置 goland 的默认终端为 bash,不然后面执行脚本的时候,可能会不支持。🎜
golangci-lint应用
🎜由于现在采用 git 作为我们的版本管理系统(VCS),而git在执行一些操作之前,允许执行脚本,这就可以让我们来执行一些操作前的代码检查。🎜🎜项目代码目录:🎜
├── .githooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit
│   ├── pre-merge-commit.sample
│   ├── pre-push
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   ├── prepare-commit-msg.sample
│   ├── push-to-checkout.sample
│   └── update.sample
├── .golangci.yml
├── go.mod
├── golangci-lint.sh
└── init.sh
  1. 可以通过项目结构看到,需要在项目根目录增加一个 .githooks 文件夹,.githooks 文件夹,

  2. 然后增加 .golangci.yml golangci-lint 使用的配置文件,

  3. 增加一个 手动执行goalngci-lint的执行脚本golangci-lint.sh

  4. 最后就是项目应用 git hooks 的脚本init.sh,用于初始化这个项目的脚本。

说了这么多,还不知道这个到底是干啥的,先来看一下效果图

commit的时候会帮助我们进行文件的 fmt:

golangci-lint应用

push

🎜🎜然后增加 .golangci.yml golangci-lint 使用的配置文件,🎜🎜🎜🎜增加一个 手动执行goalngci-lint的执行脚本golangci-lint.sh,🎜🎜🎜🎜最后就是项目应用 git hooks 的脚本init.sh,用于初始化这个项目的脚本。🎜

说了这么多,还不知道这个到底是干啥的,先来看一下效果图🎜

commit的时候会帮助我们进行文件的 fmt:🎜

golangci-lint应用

push的时候会检查整个项目是否存在有问题的地方:🎜

golangci-lint应用

如果项目存在可能的问题,那么是不会让你 push 的。通过这种方式来保证服务器上的代码都是符合规则的。

使用方式:

1. 项目中已经存在这些内容

首次通过执行 init.sh 脚本进行项目初始化设置。

golangci-lint应用

这会检查你的环境,如果一些工具不存在,它会自动下载。并会修改默认 git 钩子指向当前项目的 .githooks 文件夹。

好了,就这样,就是这么简单。

2. 新建项目这个怎么搞

这都是小问题,复制内容过去吧。

但是在复制这些之前,你一定已经是在一个git 管理的根目录下。

那么下面就开始你的复制吧。

.githooks/pre-commit:

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

# 获取所有变化的go文件
STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep .go$)

if [ "$STAGED_GO_FILES" = "" ]; then
    exit 0
fi

echo "check gofmt ..."
CHECK_FMT=$(gofmt -s -w -l $STAGED_GO_FILES)
if [ -n "${CHECK_FMT##* }" ]; then
    echo
    echo -e "these files will be\033[32m gofmt\033[0m formatted:"
    for file in ${CHECK_FMT[*]}; do
        echo -e "\033[36m\t$file\033[0m"
    done
    git add ${CHECK_FMT//\/n/ }
    echo
fi

echo "check goimports ..."
CHECK_GOPLS=$(goimports -l -w $STAGED_GO_FILES)
if [ -n "${CHECK_GOPLS##* }" ]; then
    echo
    echo -e "these files will be\033[32m goimports\033[0m formatted:"
    for file in ${CHECK_GOPLS[*]}; do
        echo -e "\033[36m\t$file\033[0m"
    done
    git add ${CHECK_GOPLS//\/n/ }
    echo
fi

printf "\033[32m COMMIT SUCCEEDED \033[0m\n"
echo

exit 0

.githooks/pre-push:

#!/bin/sh

# An example hook script to verify what is about to be pushed.  Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed.  If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
#   <local ref> <local oid> <remote ref> <remote oid>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).

#remote="$1"
#url="$2"
printf "\033[32m 推送前需要检查当前项目可以 go build 通过 \033[0m\n"

echo "run golangci-lint..."
echo

# 运行 golangci-lint 检查工具
golangci-lint run ./...
if [ $? -ne 0 ]; then
    printf "\033[31m PUSH FAILED \033[0m\n"
    exit 1
fi

printf "\033[32m PUSH SUCCEEDED \033[0m\n"
echo

exit 0

golangci-lint.sh

#!/bin/sh

if ! command -v golangci-lint &>/dev/null; then
    echo "golangci-lint not installed or available in the PATH" >&2
    echo "install golangci-lint ..." >&2
    go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1
fi

#goland 可直接定位文件
golangci-lint run ./... |sed &#39;s/\\/\//g&#39;

init.sh

#!/bin/sh

# 检查 go 是否安装
checkGoEnv() {
    # go是否安装
    if ! command -v go &>/dev/null; then
        echo "go not installed or available in the PATH" >&2
        echo "please check https://golang.google.cn" >&2
        exit 1
    fi

    # go proxy 是否设置
    if [ -z $GOPROXY ]; then
        echo "go proxy not set in the PATH" >&2
        echo "please set GOPROXY, https://goproxy.cn,direct || https://goproxy.io,direct" >&2
        exit 1
    fi

    echo "go env installed ..."
}

# 检查 go 相关工具包是否安装
checkGoLintEnv() {
    if ! command -v goimports &>/dev/null; then
        echo "goimports not installed or available in the PATH" >&2
        echo "install goimports ..." >&2
        go install golang.org/x/tools/cmd/goimports@latest
        checkGoLintEnv
        return
    fi

    echo "goimports installed ..."
}

# 检查 golangci-lint 是否安装
checkCiLintEnv() {
    if ! command -v golangci-lint &>/dev/null; then
        echo "golangci-lint not installed or available in the PATH" >&2
        echo "install golangci-lint ..." >&2
        go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1
        checkCiLintEnv
    fi

    echo "golangci-lint installed ..."
}

# 初始化钩子配置
initHooks() {
    # 如果当前目录不存在 .githooks 目录,说明位置不对
    if [ ! -d ".githooks" ]; then
        echo "exec incorrect position"
        exit 1
    fi

    # 检查是否已经设置了
    exist=$(git config core.hooksPath)
    if [ -z $exist ]; then
        # 设置 hooks 默认位置
        git config core.hooksPath .githooks
        echo "init git hooks ..." >&2
    fi
}

main() {
    checkGoEnv
    checkGoLintEnv
    checkCiLintEnv
    initHooks
}

main

.golangci.yml

run:
  timeout: 2m
  tests: false

linters:
  disable-all: true
  enable:
    - typecheck
    - staticcheck
    - govet
    - gocritic

linters-settings:
  govet:
    check-shadowing: true
    disable-all: true
    enable:
      - asmdecl
      - assign
      - atomic
      - atomicalign
      - bools
      - buildtag
      - cgocall
      - composites
      - copylocks
      - httpresponse
      - loopclosure
      - lostcancel
      - nilfunc
      - nilness
      - printf
      - shadow
      - shift
      - stdmethods
      - structtag
      - tests
      - unmarshal
      - unreachable
      - unsafeptr
      - unusedresult

  staticcheck:
    go: "1.17"
    checks: [ "all", "-SA3*", "-SA6000", "-SA6001", "-SA6003", "-ST*", "ST1006", "ST1008", "ST1016", "-QF1" ]

  gocritic:
    enabled-tags:
      - diagnostic
      - experimental
      - opinionated
      - style
    enabled-checks:
      - sliceClear
    disabled-tags:
      - performance
    disabled-checks:
      - assignOp
      - badLock
      - badRegexp
      - codegenComment
      - commentFormatting
      - commentedOutCode
      - docStub
      - dupArg
      - dupBranchBody
      - dupCase
      - dupImport
      - exitAfterDefer
      - externalErrorReassign
      - flagDeref
      - hexLiteral
      - ifElseChain
      - importShadow
      - initClause
      - mapKey
      - nestingReduce
      - newDeref
      - redundantSprint
      - regexpMust
      - regexpPattern
      - regexpSimplify
      - ruleguard
      - sloppyLen
      - sloppyTypeAssert
      - sortSlice
      - sprintfQuotedString
      - sqlQuery
      - stringConcatSimplify
      - syncMapLoadAndDelete
      - tooManyResultsChecker
      - typeDefFirst
      - typeUnparen
      - underef
      - unlabelStmt
      - unlambda
      - unnecessaryBlock
      - unnecessaryDefer
      - yodaStyleExpr
      - whyNoLint
      - paramTypeCombine
      - emptyStringTest

好了,现在你应该是这样的结构了吧

项目代码目录:

├── .githooks
│   ├── pre-commit
│   └── pre-push
├── .golangci.yml
├── golangci-lint.sh
└── init.sh

如果不是,请返回到上面在看一下步骤。

这个时候可以执行 init.sh 脚本来初始化了。

最后可以在 https://github.com/ywanbing/golangci仓库中获取代码。

以上是golangci-lint应用的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:Golang菜鸟。如有侵权,请联系admin@php.cn删除
Golang vs. Python:利弊Golang vs. Python:利弊Apr 21, 2025 am 12:17 AM

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Golang和C:并发与原始速度Golang和C:并发与原始速度Apr 21, 2025 am 12:16 AM

Golang在并发性上优于C ,而C 在原始速度上优于Golang。1)Golang通过goroutine和channel实现高效并发,适合处理大量并发任务。2)C 通过编译器优化和标准库,提供接近硬件的高性能,适合需要极致优化的应用。

为什么要使用Golang?解释的好处和优势为什么要使用Golang?解释的好处和优势Apr 21, 2025 am 12:15 AM

选择Golang的原因包括:1)高并发性能,2)静态类型系统,3)垃圾回收机制,4)丰富的标准库和生态系统,这些特性使其成为开发高效、可靠软件的理想选择。

Golang vs.C:性能和速度比较Golang vs.C:性能和速度比较Apr 21, 2025 am 12:13 AM

Golang适合快速开发和并发场景,C 适用于需要极致性能和低级控制的场景。1)Golang通过垃圾回收和并发机制提升性能,适合高并发Web服务开发。2)C 通过手动内存管理和编译器优化达到极致性能,适用于嵌入式系统开发。

golang比C快吗?探索极限golang比C快吗?探索极限Apr 20, 2025 am 12:19 AM

Golang在编译时间和并发处理上表现更好,而C 在运行速度和内存管理上更具优势。1.Golang编译速度快,适合快速开发。2.C 运行速度快,适合性能关键应用。3.Golang并发处理简单高效,适用于并发编程。4.C 手动内存管理提供更高性能,但增加开发复杂度。

Golang:从Web服务到系统编程Golang:从Web服务到系统编程Apr 20, 2025 am 12:18 AM

Golang在Web服务和系统编程中的应用主要体现在其简洁、高效和并发性上。1)在Web服务中,Golang通过强大的HTTP库和并发处理能力,支持创建高性能的Web应用和API。2)在系统编程中,Golang利用接近硬件的特性和对C语言的兼容性,适用于操作系统开发和嵌入式系统。

Golang vs.C:基准和现实世界的表演Golang vs.C:基准和现实世界的表演Apr 20, 2025 am 12:18 AM

Golang和C 在性能对比中各有优劣:1.Golang适合高并发和快速开发,但垃圾回收可能影响性能;2.C 提供更高性能和硬件控制,但开发复杂度高。选择时需综合考虑项目需求和团队技能。

Golang vs. Python:比较分析Golang vs. Python:比较分析Apr 20, 2025 am 12:17 AM

Golang适合高性能和并发编程场景,Python适合快速开发和数据处理。 1.Golang强调简洁和高效,适用于后端服务和微服务。 2.Python以简洁语法和丰富库着称,适用于数据科学和机器学习。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),