VS Code BrokenImport错误不消失golang

huangapple go评论85阅读模式
英文:

VS Code BrokenImport error doesn't disappear golang

问题

我有一个令人烦恼的错误消息,即使go成功运行脚本,它也不会消失。我已经更新了go工具。我检查了错误消息中指定的目录,实际上发现这里没有下载模块的包文件,但它们下载到了$GOPATH/pkg目录下。为了摆脱这个问题,我应该怎么做?

VS Code BrokenImport错误不消失golang

英文:

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?

VS Code BrokenImport错误不消失golang

答案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插件。

  1. 确保你的工具是最新的:按下ctrl + shift + p,输入Go,选择Install/Update tools
  2. 尝试将你的项目移出GOPATH,并为其设置go.mod文件。
  3. 重新启动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.

  1. Make sure your tools are up to date: run ctrl + shift + p,
    type Go and chose Install/Update tools.
  2. Try moving your project out of the GOPATH, and setting up go.mod for
    it.
  3. 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

huangapple
  • 本文由 发表于 2022年1月12日 11:52:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/70676114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定