VS Code throwing error 'No package for import' but code compiles and runs fine

Viewed 4344

As a work requirement learning gRPC through an online course.

I have a project defined in folder greet (outside of GOPATH) with three packages called:

  • greet_client
  • greet_server
  • greetpb

enter image description here

In the go.mod file at the root of my project, I've specified the following:

module example.com/myuser/myproject

go 1.14

The code in greet_server/server.go makes a reference to greetpb.

I'm referencing it like the following:

enter image description here

I'm able to run server.go successfully. It returns the expected the result: enter image description here

My question is on the red squiggly lines VSCode throws saying it could not import greetpb: enter image description here

Here's how package greetpb is defined (it's an auto generated file): enter image description here

How can I get rid of this warning message? Is it something I've not setup properly?

Update:

When I try to ctrl+click to view the module greetpb on the file server.go, I note that it's pointing to the url pkg.go.dev.

How can I make the program to do a "local" lookup? enter image description here

1 Answers
  1. Why would you use example.com? You don't have your package defined there.

  2. Initialize go modules first i.e.

go mod init github.com/$USER/$REPO

  1. Go mod vendor

go mod tidy && go mod vendor

  1. That's it.
Related