cannot find package google.golang.org/grpc

5.2k Views Asked by At

When I try to import the grpc package I get the following error:

could not import google.golang.org/grpc (cannot find package "google.golang.org/grpc" in any of 
    /usr/local/go/src/google.golang.org/grpc (from $GOROOT)
    /home/ansh/Go/src/google.golang.org/grpc (from $GOPATH))

This is my gopath (incase it helps):

export GOPATH="$HOME/Go"
export PATH="$PATH:/usr/local/go/bin:$GOPATH/bin"

I did install these two packages:

$ go install google.golang.org/protobuf/cmd/[email protected]
$ go install google.golang.org/grpc/cmd/[email protected]

But it doesnt seem to work

3

There are 3 best solutions below

0
Endre Simo On

In case you are using go modules, there is no need to set GOPATH anymore. However if GOPATH is set and you want to use a package from your project directory you need to set GO111MODULE=off, because by default GO111MODULE is set as ON. So even if the package google.golang.org/grpc is in your GOPATH you will have to force Go not to use the Go modules.

So something like this should work in case you are still opting of using GOPATH.

$ GO111MODULE=off && go install google.golang.org/protobuf/cmd/[email protected]
$ GO111MODULE=off && go install google.golang.org/grpc/cmd/[email protected]
0
Lakshmi Pathy On

in case grpc with updated gopath, dependencies will be looking gopath Step1 : create go mod file in base folder of the project "go mod init" Step2 : run "go mod tidy" which loads the dependencies to "go.sum"

0
Haris Beg On

If you haven't defined a go.mod file yet, please run the "go mod init ..." command to create it. Then run the following command: go get -u google.golang.org/grpc