英文:
VS Code BrokenImport error doesn't disappear golang
问题
我有一个令人烦恼的错误消息,即使go成功运行脚本,它也不会消失。我已经更新了go工具。我检查了错误消息中指定的目录,实际上发现这里没有下载模块的包文件,但它们下载到了$GOPATH/pkg目录下。为了摆脱这个问题,我应该怎么做?
英文:
I have this irritating error message that doesn't go away even though go runs the script successfully. I have already updated go tools. I checked out directories that are specified in this error message and actually it turns out that there are no package files here for downloaded modules, but they downloaded under $GOPATH/pkg directory. What should I do in order to get rid of this?
答案1
得分: 2
你需要从包含 go.mod 文件(模块根目录)或主模块的子目录中打开。如果你不能这样做(例如因为你必须使用多个模块),你需要选择使用实验模式,并设置 "gopls" 的 { "build.experimentalWorkspaceModule": true }。这是 Go 语言服务器当前的已知限制。由 Hana 回答。
英文:
You need to open from a directory with go.mod file (module root) or a subdirectory of the main module. If you cannot (e.g. because you have to work with multiple modules), you will need to opt in to use the experimental mode with "gopls": { "build.experimentalWorkspaceModule": true } setting. This is a known limitation of the Go language server currently.
Answer by Hana
答案2
得分: 0
默认情况下,我们所有的包都会下载到$GOPATH/pkg
目录中。
你遇到的问题是由于VSCode中的“同步”问题导致的。有时候,VSCode在同步时需要几秒钟的时间,同时会显示一个错误。
你可以通过重新启动VSCode来解决这个问题,但请确保你已经按照官方文档设置了Golang的VSCode插件。
- 确保你的工具是最新的:按下
ctrl
+shift
+p
,输入Go,选择Install/Update tools
。 - 尝试将你的项目移出GOPATH,并为其设置
go.mod
文件。 - 重新启动VSCode编辑器。
在运行go get package_name
之后,确保按下ctrl
+s
保存文件,即使你已经打开了自动保存功能。1或2秒后,右下角会弹出一个提示,表示正在保存更改,错误将会消失。
这就是为什么当你在新窗口中打开代码时错误会消失的原因。
英文:
By default all our packages are downloaded into $GOPATH/pkg
directory.
The issue that you are facing is happened due to sync
issue in VSCode. Sometimes VSCode takes few second for sync meanwhile it will show an error.
You can get rid of with this issue by restarting the VSCode but make sure you have followed official docs Golang for VSCode.
- Make sure your tools are up to date: run
ctrl
+shift
+p
,
type Go and choseInstall/Update tools
. - Try moving your project out of the GOPATH, and setting up
go.mod
for
it. - Restart the VSCode editor
Make sure after go get package_name
you should press ctrl
+s
though if you have turned on autosave. After 1 or 2 second popup will occur down right side saying changes are being saved and error will move away.
This is why when you opened code in new window error is gone.
答案3
得分: 0
确保 go.mod
文件位于工作区根目录。https://github.com/golang/go/issues/49715
英文:
Make sure go.mod
file is in the workspace root. https://github.com/golang/go/issues/49715
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论