预提交会打印“ golint:找不到命令”

时间:2020-10-28 06:29:46

标签: go

Envs

$ go version
go version go1.15.2 linux/amd64

我想要什么

我想制作一个由Go实现的微服务。

发生了什么

当我运行git commit时,预提交运行golint命令,现在它会显示“ golint:找不到命令”。

asuha on asuha-HP-EliteDesk-800-G4-TWR in ~/go/src/github.com/Asuha-a/URLShortener/api/services/user(27m|feat/_20_design_backend_architecture*)
$ git commit -m "feat: add user app #20"
go fmt...................................................................Passed
go lint..................................................................Failed
- hook id: go-lint
- exit code: 1

/home/asuha/.cache/pre-commit/repo5ywtpl6j/run-go-lint.sh: line 7: golint: command not found

go imports...............................................................Passed
go-cyclo.................................................................Failed
- hook id: go-cyclo
- exit code: 127

/home/asuha/.cache/pre-commit/repo5ywtpl6j/run-go-cyclo.sh: line 9: exec: gocyclo: not found

validate toml........................................(no files to check)Skipped
Check files aren't using go's testing package........(no files to check)Skipped
go-unit-tests............................................................Passed
go-mod-tidy..............................................................Passed

代码

.zshrc中的设置

export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin

项目树

asuha on asuha-HP-EliteDesk-800-G4-TWR in ~/go/src/github.com/Asuha-a/URLShortener(43m|feat/_20_design_backend_architecture*)
$ tree
.
├── api
│   ├── Dockerfile
│   ├── go.mod
│   ├── go.sum
│   ├── main.go
│   ├── pb
│   └── services
│       ├── README.md
│       └── user
│           ├── go.mod
│           ├── go.sum
│           └── main.go
├── docker-compose.yml
└── README.md

gopath中的树

.
├── bin
│   ├── gocyclo
│   ├── golint
│   ├── gopls
│   ├── go-test
│   ├── my-first-go
│   ├── protoc-gen-go
│   ├── protoc-gen-go-grpc
│   └── user
├── pkg
│   ├── linux_amd64
│   │   └── github.com
│   ├── mod
│   │   ├── cache
│   │   ├── cloud.google.com
│   │   ├── github.com
│   │   ├── golang.org
│   │   ├── google.golang.org
│   │   ├── gopkg.in
│   │   ├── honnef.co
│   │   └── mvdan.cc
│   └── sumdb
│       └── sum.golang.org
└── src
    ├── github.com
    │   ├── Asuha-a
    │   ├── fzipp
    │   ├── gin-contrib
    │   ├── gin-gonic
    │   ├── golang
    │   ├── go-playground
    │   ├── leodido
    │   ├── mattn
    │   └── ugorji
    ├── golang.org
    │   └── x
    ├── google.golang.org
    │   └── protobuf
    └── gopkg.in
        └── yaml.v2

.pre-commit-config.yaml

repos:
- repo: git://github.com/dnephin/pre-commit-golang
  rev: master
  hooks:
    - id: go-fmt
    - id: go-lint
    - id: go-imports
    - id: go-cyclo
      args: [-over=15]
    - id: validate-toml
    - id: no-go-testing
    - id: go-unit-tests
    - id: go-mod-tid

/home/asuha/go/src/github.com/Asuha-a/URLShortener/api/go.mod

module github.com/Asuha-a/URLShortener/api

go 1.15

require (
    github.com/fzipp/gocyclo v0.3.1 // indirect
    github.com/gin-gonic/gin v1.6.3
    github.com/go-playground/validator/v10 v10.4.1 // indirect
    github.com/golang/protobuf v1.4.3 // indirect
    github.com/json-iterator/go v1.1.10 // indirect
    github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
    github.com/modern-go/reflect2 v1.0.1 // indirect
    github.com/ugorji/go v1.1.13 // indirect
    golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
    golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
    golang.org/x/sys v0.0.0-20201026173827-119d4633e4d1 // indirect
    golang.org/x/tools v0.0.0-20201028025901-8cd080b735b3 // indirect
    google.golang.org/protobuf v1.25.0 // indirect
    gopkg.in/yaml.v2 v2.3.0 // indirect
)

/home/asuha/go/src/github.com/Asuha-a/URLShortener/api/services/user/go.mod

module github.com/Asuha-a/URLShortener/api/services/user

go 1.15

require (
    github.com/fzipp/gocyclo v0.3.1 // indirect
    golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
    golang.org/x/tools v0.0.0-20201028025901-8cd080b735b3 // indirect
)

我想知道的事情

为什么不能穿刺呢? 如何解决?

1 个答案:

答案 0 :(得分:2)

您需要为PATH env变量提供并保留更改。如果您使用bash,则可以将下一个更改添加到.bashrc.bash_profile(取决于操作系统)。

export GOPATH="${HOME}/go"
export GOROOT="/usr/local/opt/go/libexec"

if [[ $PATH != *$GOPATH* ]]; then
    export PATH="${GOPATH}/bin:${PATH}"
fi

if [[ $PATH != *$GOROOT* ]]; then
    export PATH="${GOROOT}/bin:${PATH}"
fi

注意:在我的情况下,有$HOME个变量,但是您可以将完整路径写入gopath。