1

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/protoc-gen-go@v1.26
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1

But it doesnt seem to work

Ansh Malik
  • 162
  • 2
  • 7
  • 3
    You shouldn't need to rely on GOPATH anymore. Use go modules. Try reading [Get started with Go](https://go.dev/doc/tutorial/getting-started) and take notice of the use of the **`go mod`** tool. Other useful articles: [Create a Go module](https://go.dev/doc/tutorial/create-module) and [Using Go Modules](https://go.dev/blog/using-go-modules). – mkopriva Feb 01 '22 at 08:44
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 11 '22 at 03:09

3 Answers3

0

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/protoc-gen-go@v1.26
$ GO111MODULE=off && go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1
Endre Simo
  • 11,330
  • 2
  • 40
  • 49
0

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

Haris Beg
  • 49
  • 3
-1

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"