【发布时间】:2021-05-31 01:13:17
【问题描述】:
我遇到了可能是 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 扩展并尝试重新启动语言服务器,但这并没有改变任何东西。错误消息出现在每个目录中的所有外部包导入语句中。
我很乐意得到一些建议。
【问题讨论】:
-
您的 GOPATH 设置是否正确?无论是在 Windows 上还是在 VSCode 上。
-
@dubonzi,据我所知,VSCode 使用 go env 文件中的 GOPATH,我的设置为 C:\Users\max\go,项目目录为 C:\Users\max\ go\src\github.com\kozie1337\projectname 所以应该是对的。
-
你应该使用模块而不是 GOPATH。 GOPATH 版本已弃用。
标签: go go-modules