英文:
VSCode cannot find package in new GO installation
问题
我刚刚在一台新电脑上安装了Go和Visual Studio Code以及相关工具。当我访问我的现有项目时,出现了导入问题,例如:
无法导入 golang.org/x/text/encoding/charmap(在以下任何位置都找不到包“golang.org/x/text/encoding/charmap”:
C:\Program Files\Go\src\golang.org\x\text\encoding\charmap(来自$GOROOT)
C:\Users\allan\go\src\golang.org\x\text\encoding\charmap(来自$GOPATH))
GOPATH=C:\Users\allan\go
操作系统版本:Windows 10.0.19043 Build 19043
Go版本:go1.17.1 windows/amd64
Visual Studio Code版本:1.60.2(用户安装)
实际上,该包已经通过go get ..
安装了,并且已经安装在以下位置:
c:\Users\allan\go\pkg\mod\golang.org\x\text@v0.3.7\encoding\charmap\charmap.go
我注意到两件事情:
-
编译器寻找的是
C:\Users\allan\go\src\golang.org\...
而不是..\pkg\mod\golang.org\...
-
安装的包的名称是
text@v0.3.7
而不仅仅是text
。
然而,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=
所以,我猜编译器应该能够找到要使用的版本。
项目文件夹go.mod
:
module github.com/Orionsg/util
go 1.16
require golang.org/x/text v0.3.7
以及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=
Visual Studio Code或其他Go工具是否与Go安装不同步?
有什么我可以做的,让Go编译器使用正确的包目录位置?
还有其他建议吗?
英文:
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:
-
The compiler looks for
C:\Users\allan\go\src\golang.org\...
rather than..\pkg\mod\golang.org\...
-
The package installed has the name
text@v0.3.7
rather than justtext
.
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?
答案1
得分: 4
我终于找到问题所在了。这是一个VSCode的问题:
在VSCode中打开文件夹时,它不再支持打开一个顶层文件夹,然后在其下面打开项目文件夹,可以展开和关闭不同项目的访问。这在之前是正常工作的。
现在,每个项目都必须单独作为顶层文件夹打开,否则导入检查将无法正常工作。
英文:
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.
答案2
得分: 0
如果Go项目位于主项目的子文件夹中,则将Go项目文件夹添加到工作区(文件->添加文件夹到工作区)以解决此问题。更多信息请参见这里。
英文:
If the Go project is in a sub folder of main project, then add the Go project folder to workspace(Files->Add folder to workspace) to fix this. More info here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论