我遇到了可能是 Gopls 语言服务器问题:在 VSCode 中使用带有 Go 扩展的 Go 模块时,我的所有外部包导入语句都被标记为不正确。这正是我到目前为止所做的:
在我的 GOPATH/src/github.com/Kozie1337/projectname 中:
- run
go mod init github.com/Kozie1337/projectname
- run
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
import 语句,我收到错误:
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 模块go\src
即使它们存储在go\pkg\mod
现在。 VSCode/Gopls 是否有一些与此相关的配置文件,或者我做错了什么?我以前从未使用过 Go/Go 模块。
尽管存在 linting 错误,导入和代码实际上仍然有效,但该错误会禁用所有自动完成功能,因此仅忽略它并不是一个可行的解决方案。
我重新安装了 VSCode 的 Go 扩展并尝试重新启动语言服务器,但这没有改变任何东西。该错误消息出现在每个目录中的所有外部包导入语句中。
我很乐意提供一些建议。