Home > Blockchain >  VSCode cannot find package in new GO installation
VSCode cannot find package in new GO installation

Time:10-04

I have just installed Go and Visual Studio Code with tools on a new computer. When accessing my existing projects, I get import problems, e.g.:

could not import golang.org/x/text/encoding/charmap (cannot find package \"golang.org/x/text/encoding/charmap\" in any of  
C:\Program Files\Go\src\golang.org\x\text\encoding\charmap (from $GOROOT)
C:\Users\allan\go\src\golang.org\x\text\encoding\charmap (from $GOPATH))

GOPATH=C:\Users\allan\go  
OS version: Windows 10.0.19043 Build 19043  
GO version: go1.17.1 windows/amd64  
Visual Studio Code version: 1.60.2 (user setup)

The package has actually been installed with go get ..
And it has been installed in:

c:\Users\allan\go\pkg\mod\golang.org\x\text@v0.3.7\encoding\charmap\charmap.go

Two things that I notice:

  1. The compiler looks for C:\Users\allan\go\src\golang.org\... rather than ..\pkg\mod\golang.org\...

  2. The package installed has the name [email protected] rather than just text.
    However, the go.sum file includes:

    golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=  
    golang.org/x/text v0.3.7/go.mod h1:u 2 /6zg i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=  

So, I guess the compiler should figure out the version to use.


Project folder go.mod:

module github.com/Orionsg/util

go 1.16

require golang.org/x/text v0.3.7

And go.sum:

golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u 2 /6zg i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

Is Visual Studio Code or some other Go tool out of sync with the Go installation?

Is there something I can do to make the Go compiler use the correct directory location of packages?

Any other suggestions?

CodePudding user response:

I finally found out what was wrong. It is a VSCode issue:

When opening folders in VSCode, it no longer supports opening a top level folder with project folders below it that one can expand and close as one acesseses different projects. This worked fine earlier.

Now, one has to open each project individually as a top level folder, or the check on imports does not work correctly.

  • Related