如何解决Go Modules 在 VSCode 中导入问题“在 [...] 中的任何一个中都找不到包 [...]”
我遇到了似乎是 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" // 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
}
[...]
将鼠标悬停在 github.com/gorilla/mux
导入语句上时,出现错误:
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))"
它看起来像是在从没有从 go\src
导入 Go 模块的情况下寻找包,即使它们现在存储在 go\pkg\mod
中。是否有一些 VSCode/Gopls 的配置文件与此有关,还是我做错了什么?我以前从未使用过 Go/Go 模块。
尽管存在 linting 错误,但导入和代码实际上仍然有效,但该错误会禁用所有自动完成功能,因此仅忽略它不是可行的解决方案。
我重新安装了 VSCode 的 Go 扩展并尝试重新启动语言服务器,但这没有任何改变。错误消息出现在每个目录中的所有外部包导入语句中。
我很乐意为您提供一些建议。
解决方法
官方的 go 模块 blog post 专门说“某处外面 $GOPATH/src,”。
所以在 GOPATH 之外初始化你的 go 模块。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。