【问题标题】:pre-commit prints 'golint: command not found'预提交打印'golint:找不到命令'
【发布时间】:2021-02-10 12:04:54
【问题描述】:

环境

$ go version
go version go1.15.2 linux/amd64

我想要什么

我想做一个用 Go 实现的微服务。

发生了什么

当我运行 git commit 时,预先提交运行 golint 命令,现在它会打印 'golint: command not found'。

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 中的 go 设置

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
)

我想知道的

为什么 golint 不能运行? 如何解决?

【问题讨论】:

    标签: go pre-commit.com


    【解决方案1】:

    您需要获取并保留对 PATH 环境变量的更改。如果您使用 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 的完整路径。

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 1970-01-01
      • 2015-08-23
      • 2018-01-26
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      相关资源
      最近更新 更多