linter verwendet Tools, um einige Überprüfungen des Codes durchzuführen und so die Qualität des übermittelten Codes sicherzustellen. golangci-lint 是一个 Go linters 聚合器,而 linter 是使用工具来对代码提供一些检查,保证提交代码的质量。
Warum nicht golangci-lint direkt verwenden?🎜🎜Da es sich bei dem Projekt um eine Aktivität für mehrere Personen handelt, muss ich es bei der vorherigen Verwendung immer manuell ausführenbash, andernfalls wird es möglicherweise nicht unterstützt, wenn das Skript später ausgeführt wird. 🎜🎜Da wir jetzt Git als unser Versionsverwaltungssystem (VCS) verwenden ), während .githooks 文件夹,
然后增加 .golangci.yml golangci-lint 使用的配置文件,
增加一个 手动执行goalngci-lint的执行脚本golangci-lint.sh,
最后就是项目应用 git hooks 的脚本init.sh,用于初始化这个项目的脚本。
说了这么多,还不知道这个到底是干啥的,先来看一下效果图
在commit的时候会帮助我们进行文件的 fmt:
在push
🎜🎜 und dann goalngci-lints Ausführungsskriptgolangci-lint.sh, 🎜🎜🎜🎜Das letzte ist die Projektanwendunggit Hooks's script<code style="font- size: 14px;overflow-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb(30, 107 , 184);Hintergrundfarbe: rgba(27, 31, 35, 0.05);Schriftfamilie: „Operator Mono“, Consolas, Monaco, Menlo, Monospace;Wortumbruch: break-all;“>init.sh< /code>, wird zum Initialisieren dieses Projektskripts verwendet. 🎜</section></li></ol><p data-tool=" mdnice editor>Das ist alles Viele, Ich weiß immer noch nicht, wofür das ist, schauen wir uns zuerst die Renderings an🎜<p data-tool="mdnice editor" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px ;">In<code style="font-size: 14px;overflow-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;color: rgb( 30, 107, 184);Hintergrundfarbe: rgba(27, 31, 35, 0.05);Schriftfamilie: „Operator Mono“, Consolas, Monaco, Menlo, Monospace;word-break: break-all;“>commit hilft uns bei der Datei
#!/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 's/\\/\//g'
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
Schließlich können Sie den Code im https://github.com/ywanbing/golangciLager erhalten.
Das obige ist der detaillierte Inhalt vonGolangci-Lint-Anwendung. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!
Stellungnahme:
Dieser Artikel ist reproduziert unter:Golang菜鸟. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen