golangci-lint 애플리케이션
golangci-lint란 무엇인가요?
golangci-lint
는 Go 린터 수집기이고 linter
는 제출된 코드의 품질을 보장하기 위해 도구를 사용하여 코드에 대한 몇 가지 검사를 제공합니다. golangci-lint
是一个 Go linters 聚合器,而 linter
是使用工具来对代码提供一些检查,保证提交代码的质量。
为什么不直接使用 golangci-lint ?
需要手动执行,在之前使用的过程中,由于项目是多人活动,总是会忘记执行golangci-lint
进行代码检查,当前我自己也是。所以我们希望采用一种隐式的方式来自动执行。那么经过多番思考,采用 git 的 hooks 可以来自动执行一些脚本。
这期间还有一些其他的尝试,不过这个文章主要说 在git中的使用ci-lint,有兴趣的可以移步到为什么最终采用git 本地 hooks 的方式执行golangci-lint?
采用 git 的 hooks 来自动执行检查!
前提条件:
请设置 goland 的默认终端为 bash
,不然后面执行脚本的时候,可能会不支持。

由于现在采用 git 作为我们的版本管理系统(VCS),而git
golangci-lint
코드 검사용, 현재 나 자신. 그래서 우리는 그것을 자동으로 실행하기 위해 암시적인 방법을 사용하고 싶습니다. 그래서 많은 생각 끝에 git Hooks를 사용하여 일부 스크립트를 자동으로 실행할 수 있습니다. 🎜🎜이 기간 동안 다른 시도도 있었지만 이 기사에서는 주로 git에서 ci-lint 사용에 대해 설명합니다. 관심 있는 분들은 마침내 golangci를 실행하는 데 git 로컬 후크가 사용되는 이유를 살펴보겠습니다. 보푸라기? 🎜🎜🎜🎜git Hooks를 사용하여 자동으로 검사를 수행하세요! 🎜🎜전제 조건: 🎜🎜goland의 기본 터미널을 bash
, 그렇지 않으면 나중에 스크립트 실행 시 지원되지 않을 수 있습니다. 🎜git 일부 작업을 수행하기 전에 스크립트 실행을 허용하므로 일부 작업 전에 코드 검사를 수행할 수 있습니다. 🎜🎜프로젝트 코드 디렉토리: 🎜<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>├── .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</pre><div class="contentsignin">로그인 후 복사</div></div><ol class="list-paddingleft-1" data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;"><li><section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"><p style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;color: black;">는 프로젝트 구조를 통해 확인할 수 있습니다. 프로젝트 루트 디렉터리 -right: 2px;margin-left: 2px;color: rgb(30, 107, 184); background-color: rgba(27, 31, 35, 0.05);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;">.githooks
폴더, .githooks
文件夹,然后增加 .golangci.yml
golangci-lint 使用的配置文件,
增加一个 手动执行goalngci-lint
的执行脚本golangci-lint.sh
,
最后就是项目应用 git hooks
的脚本init.sh
,用于初始化这个项目的脚本。
说了这么多,还不知道这个到底是干啥的,先来看一下效果图
在commit
的时候会帮助我们进行文件的 fmt
:

在push
.golangci.yml
golangci-에서 사용하는 구성 파일 lint, 🎜< /section>🎜🎜🎜수동 실행 추가goalngci-lint
실행 스크립트golangci-lint.sh
, 🎜🎜🎜🎜마지막은 프로젝트 애플리케이션입니다<코드 스타일 ="글꼴 크기: 14px ;오버플로 랩: 중단 단어; 패딩: 2px 4px; 테두리 반경: 4px; 여백 오른쪽: 2px; 여백 왼쪽: 2px; 색상: rgb(30, 107, 184); background-color: rgba(27 , 31, 35, 0.05);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;">git 후크 scriptinit.sh< /code>, 이 프로젝트 스크립트를 초기화하는 데 사용됩니다. 🎜</section></li></ol><p data-tool="mdnice editor" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;">그렇습니다. 아직 이게 무슨 용도인지는 모르겠습니다. 먼저 렌더링을 살펴보겠습니다🎜<p data-tool="mdnice editor" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px ;"><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);배경색: rgba(27, 31, 35, 0.05);글꼴 계열: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;">커밋
는 fmt
:🎜
In푸시
하면 전체 프로젝트에 문제가 있는지 확인됩니다. 🎜
如果项目存在可能的问题,那么是不会让你 push 的。通过这种方式来保证服务器上的代码都是符合规则的。
使用方式:
1. 项目中已经存在这些内容
首次通过执行 init.sh
脚本进行项目初始化设置。

这会检查你的环境,如果一些工具不存在,它会自动下载。并会修改默认 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 '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
.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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Go에서는 gorilla/websocket 패키지를 사용하여 WebSocket 메시지를 보낼 수 있습니다. 특정 단계: WebSocket 연결을 설정합니다. 문자 메시지 보내기: WriteMessage(websocket.TextMessage,[]byte("Message"))를 호출합니다. 바이너리 메시지 보내기: WriteMessage(websocket.BinaryMessage,[]byte{1,2,3})를 호출합니다.

Go에서 함수 수명주기에는 정의, 로드, 연결, 초기화, 호출 및 반환이 포함됩니다. 변수 범위는 함수 수준과 블록 수준으로 구분됩니다. 함수 내의 변수는 내부적으로 표시되지만 블록 내의 변수는 블록 내에서만 표시됩니다. .

Go에서는 정규식을 사용하여 타임스탬프를 일치시킬 수 있습니다. ISO8601 타임스탬프를 일치시키는 데 사용되는 것과 같은 정규식 문자열을 컴파일합니다. ^\d{4}-\d{2}-\d{2}T \d{ 2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . regexp.MatchString 함수를 사용하여 문자열이 정규식과 일치하는지 확인합니다.

Go와 Go 언어는 서로 다른 특성을 지닌 서로 다른 개체입니다. Go(Golang이라고도 함)는 동시성, 빠른 컴파일 속도, 메모리 관리 및 크로스 플랫폼 이점으로 유명합니다. Go 언어의 단점은 다른 언어에 비해 생태계가 덜 풍부하고 구문이 더 엄격하며 동적 타이핑이 부족하다는 점입니다.

메모리 누수로 인해 파일, 네트워크 연결, 데이터베이스 연결 등 더 이상 사용하지 않는 리소스를 닫는 방식으로 Go 프로그램 메모리가 지속적으로 증가할 수 있습니다. 더 이상 강력하게 참조되지 않는 경우 약한 참조를 사용하여 메모리 누수 및 가비지 수집 대상 개체를 방지합니다. go 코루틴을 사용하면 메모리 누수를 방지하기 위해 종료 시 코루틴 스택 메모리가 자동으로 해제됩니다.

IDE를 사용하여 Go 함수 문서 보기: 함수 이름 위에 커서를 놓습니다. 단축키(GoLand: Ctrl+Q, VSCode: GoExtensionPack 설치 후 F1을 누르고 "Go:ShowDocumentation" 선택)를 누릅니다.

단위 테스트 동시 기능은 동시 환경에서 올바른 동작을 보장하는 데 도움이 되므로 매우 중요합니다. 동시 기능을 테스트할 때는 상호 배제, 동기화, 격리와 같은 기본 원칙을 고려해야 합니다. 동시 기능은 경쟁 조건을 시뮬레이션하고, 테스트하고, 결과를 확인하여 단위 테스트할 수 있습니다.

Go의 함수에 지도를 전달하면 기본적으로 복사본이 생성되며 복사본을 수정해도 원본 지도에는 영향을 미치지 않습니다. 원본 지도를 수정해야 하는 경우 포인터를 통해 전달할 수 있습니다. 빈 맵은 기술적으로 nil 포인터이기 때문에 주의해서 처리해야 하며, 비어 있지 않은 맵을 기대하는 함수에 빈 맵을 전달하면 오류가 발생합니다.
