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

2

6

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

Canasta answered 7/8, 2020 at 6:31 Comment(0)
A
0

I know that I am late at the party but just in case someone has a similar issue.

What worked for after lots of tries was to edit the file ~/.config/Code/User/settings.json

At the line that says I made it like this. "go.goroot": "../",

In this project everything was in the one level out. For now it's ok.

Just a diclaimer, I am new to Go. So I will edit in future in case I learn something new about this.

Advantageous answered 20/3, 2023 at 16:9 Comment(0)
S
-1
  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.
Shedd answered 7/8, 2020 at 10:3 Comment(2)
example.com is fine for a test module that isn't published anywhere. go mod vendor is also not necessary.Splendid
Also they've already initialized the module, as you can tell by the presence of go.mod and go.sum filesSplendid

© 2022 - 2025 — McMap. All rights reserved.