3

Envs

$ go version
go version go1.15.2 linux/amd64

What I want

I want to make a microservice implemented by Go.

What happened

When I run git commit, pre-commit run golint command, and now it prints '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

Codes

settings for go in .zshrc

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

project tree

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

tree in 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
)

What I want to know

Why golint can't run? How to fix it?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Asuha
  • 231
  • 1
  • 5
  • 16

2 Answers2

7

You need to source and persist changes to your PATH env variable. If you using bash you can add next changes to .bashrc or .bash_profile (it's up to OS).

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

Note: there is $HOME variables in my case, but you can write full path to your gopath.

Oleg Butuzov
  • 4,795
  • 2
  • 24
  • 33
0

I added these lines directly into the pre-commit hook (at .git/hooks/pre-commit in your repo) and that fixed it for me:

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

Martin
  • 199
  • 2
  • 6