英文:
Go Modules importing issue in VSCode ("cannot find package [...] in any of [...]")
问题
我遇到了一个可能是Gopls语言服务器问题的情况:当我在VSCode中使用Go扩展和Go模块时,所有的外部包导入语句都被标记为错误。以下是我目前所做的事情:
在我的GOPATH/src/github.com/Kozie1337/projectname目录下:
- 运行
go mod init github.com/Kozie1337/projectname
- 运行
go get -u github.com/gorilla/mux
在go.main文件中:
package main
import (
"log"
"net/http"
"github.com/gorilla/mux" // 这一行被标记为错误,下面是错误信息
)
func main() {
r := mux.NewRouter() // 这实际上是可以工作的,尽管go linter说mux没有被导入
http.ListenAndServe(":9000", r)) // 服务器也可以启动mux路由
}
[...]
当我悬停在github.com/gorilla/mux
导入语句上时,我得到以下错误:
无法导入github.com/gorilla/mux(无法在以下任何位置找到包“github.com/gorilla/mux”:
C:\Program Files\Go\src\github.com\gorilla\mux(来自$GOROOT)
C\src\github.com\gorilla\mux(来自$GOPATH)
\Users\max\go\src\github.com\gorilla\mux(来自$GOPATH))"
看起来它正在寻找以前从go\src
导入的包,尽管它们现在存储在go\pkg\mod
中。是否有关于这个问题的VSCode/Gopls的配置文件,或者我做错了什么?我以前从未使用过Go/Go模块。
尽管有linting错误,导入和代码实际上是可以工作的,但这个错误会禁用所有的自动完成,所以忽略它并不是一个可行的解决方案。
我已经重新安装了VSCode的Go扩展,并尝试重新启动语言服务器,但没有改变任何东西。错误消息出现在每个目录的所有外部包导入语句中。
我希望能得到一些建议。
英文:
I'm encountering what probably seems to be a Gopls language server issue: All my external package import statements are being marked as incorrect when using Go Modules with the Go extension in VSCode. Here's exactly what I did so far:
Inside my GOPATH/src/github.com/Kozie1337/projectname:
- run
go mod init github.com/Kozie1337/projectname
- run
go get -u github.com/gorilla/mux
Inside go.main:
package main
import (
"log"
"net/http"
"github.com/gorilla/mux" // This is being marked as wrong with the err. msg. down below
)
func main() {
r := mux.NewRouter() // This actually works, even though the go linter says that mux isn't imported
http.ListenAndServe(":9000", r)) // server starts too with mux routes
}
[...]
When hovering over the github.com/gorilla/mux
import statement, I'm getting the error:
could not import github.com/gorilla/mux (cannot find package "github.com/gorilla/mux" in any of
C:\Program Files\Go\src\github.com\gorilla\mux (from $GOROOT)
C\src\github.com\gorilla\mux (from $GOPATH)
\Users\max\go\src\github.com\gorilla\mux (from $GOPATH))"
It looks like it's looking for the packages the way they were imported without Go modules from go\src
even though they are stored in go\pkg\mod
now. Is there some config file for VSCode/Gopls regarding this, or am I doing something wrong? I've never used Go/Go Modules before.
The import and code actually works despite the linting error, but the error disables all autocompletion so just ignoring it is not a viable solution.
I reinstalled to Go extension for VSCode and tried restarting the language server but that didn't change anything. The error message appears at all external package import statements, in every directory.
I'd be glad for some advice.
答案1
得分: 6
官方的Go模块博客文章明确指出:“在$GOPATH/src之外的某个地方”。
因此,请在GOPATH之外初始化您的Go模块。
英文:
The official go modules blog post specifically says "somewhere outside $GOPATH/src,".
So initialize you go module outside GOPATH.
答案2
得分: 5
同样的错误可能会出现在Go项目位于主项目目录的子目录中的情况下。要解决这个问题,要么在工作区根目录中打开Go项目,要么在VScode中将项目添加到工作区。更多信息请参见这里。
英文:
Same error can come if the Go project is in a subdirectory of the main project directory. To solve this, either open the Go project in workspace root or add the project to workspace in VScode. See this for more info.
答案3
得分: 4
我意识到这是一个旧问题,但自从go 1.18和gopls@0.8.0以来,它们引入了一种原生的方式来支持这一点,使用go工作区。
在我的情况下,我将我的make文件、docker-compose文件等放在根目录下,所有的项目都在./services文件夹下,所以我做了以下操作:
root-dir> go work init
root-dir> go work use ./services/service1 ./services/service2
这将在root-dir中添加一个go.work文件,我不再遇到上述错误。
在这里阅读更多信息:https://github.com/golang/tools/blob/master/gopls/doc/workspace.md
英文:
I realize this is an old question, but since go 1.18 and gopls@0.8.0 they have introduced a native way to support this, using go workspaces.
In my case, I have my make files, docker-compose files etc in the root and all projects under ./services folder, so I did this:
root-dir> go work init
root-dir> go work use ./services/service1 ./services/service2
This adds a go.work file to the root-dir and I no longer has the error above.
Read more here: https://github.com/golang/tools/blob/master/gopls/doc/workspace.md
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论