1

I'm Ubuntu 21.04 user and I'm trying to use "github.com/golang/protobuf/protoc-gen-go/grpc" lib and I'm confused if my $GOPATH should be GOPATH=$HOME/go or GOPATH=usr/local/go. Because I installed my grpc into GOPATH=$HOME/go, but VSCODE could not find it, just libs in usr/local/go.

May seem obvious to be usr/local/go, but I see everywhere people using $GOPATH as GOPATH=$HOME/go.

  • `/usr/local/go` is usually GOROOT. What problem are you having exactly? That isn’t a main package, so it’s not something you install. – JimB Apr 16 '22 at 01:01
  • I'm confused about where Go get libs? Because I tried to import grpc but vscode couldn't find it. But libs as net, log, fmt, etc, It can find. I get this error: '''cannot find package "github.com/golang/protobuf/protoc-gen-go/grpc" in any of: /usr/local/go/src/github.com/golang/protobuf/protoc-gen-go/grpc (from $GOROOT) /home/luizportel4/Go/src/github.com/golang/protobuf/protoc-gen-go/grpc (from $GOPATH)''' My variables env are: export GOPATH=$HOME/Go export PATH=$PATH:$GOPATH/bin export PATH=$PATH:$(go env GOPATH)/bin – Luiz Portela Apr 16 '22 at 01:22

1 Answers1

0

The PATH to the go tool should be set. However, GOROOT doesn't need to be set and will be deterined by the go tool when it runs. GOPATH defaults to $HOME/go and doesn't need to be set unless you want to move it elsewhere. With Go Modules, GOPATH is only used to store downloaded/cached modules, compiled objects, and go install binaries. Your source can live in any directory underneath a go.mod file.

  • 1
    The older question/answer is somewhat out of date now. The `PATH` to the `go` tool should be set. However, `GOROOT` doesn't need to be set and will be deterined by the `go` tool when it runs. `GOPATH` defaults to `$HOME/go` and doesn't need to be set unless you want to move it elsewhere. With Go Modules, `GOPATH` is only used to store downloaded/cached modules, compiled objects, and `go install` binaries. Your source can live in any directory underneath a `go.mod` file. – mpx Apr 16 '22 at 07:52
  • Thanks for your explanation, I changed based on your comment and in documentation, now my env is like that: `export GOPATH=$HOME/go/packages export PATH="$PATH:$(go env GOPATH)/bin"` GOPATH is set how I want to and PATH I set how is in gRPC documentation. – Luiz Portela Apr 16 '22 at 19:36
  • Looks good. I assume `/usr/local/go/bin` is in your `PATH` too. I'd probably replace `$(go env GOPATH)` with the static directory name since it shouldn't change. This avoids your path breaking if you have a problem with the Go toolchain. – mpx Apr 17 '22 at 02:05
  • Thank you! I'll edit my answer with your comment to get visibility. – Luiz Portela Apr 17 '22 at 02:13