登录
首页 >  Golang >  Go问答

VSCode:无法引入 Golang 模块

来源:stackoverflow

时间:2024-03-11 16:54:25 436浏览 收藏

今天golang学习网给大家带来了《VSCode:无法引入 Golang 模块》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

我正在 gopath 中编写一个 go 项目,并且使用 redigo 包连接到 redis 服务器。应用程序运行良好,但是在 vscode 中,包导入时出现了这个恼人的错误,这导致 vscode 无法提供智能感知建议

无法导入 github.com/gomodule/redigo/redis (导入路径 github.com/gomodule/redigo/redis 没有包数据)

这是我的 vscode settings.json

{
    "editor.fontsize": 14,
    "editor.formatonpaste": true,
    "editor.multicursormodifier": "ctrlcmd",
    "editor.snippetsuggestions": "top",
    "extensions.ignorerecommendations": false,
    "workbench.statusbar.visible": true,
    "workbench.icontheme": "vscode-great-icons",
    "files.autosave": "afterdelay",
    "go.uselanguageserver": true,
    "go.alternatetools": {
        "go-langserver": "bingo"
    },
    "go.toolsenvvars": {
        "go111module": "on"
    },
    "go.languageserverexperimentalfeatures": {
        "autocomplete": true,
        "documentsymbols": true,
        "findreferences": true,
        "format": true,
        "gotodefinition": true,
        "gototypedefinition": true,
        "hover": true,
        "signaturehelp": true,
        "rename": true,
        "workspacesymbols": true,
    },
    "go.linttool": "golangci-lint",
    "go.lintflags": [
        "--fast",
        "-e", "goimports",
        "-e", "gocritic",
        "-e", "gocyclo",
        "-e", "gosec",
        "-e", "maligned",
        "-e", "scopelint",
        "-e", "interfacer",
        "-e", "goconst",
        "-e", "unconvert",
        "-e", "unparam",
        "-e", "prealloc",
        "-e", "varcheck",
    ],
    "go.formattool": "goimports",
    "editor.minimap.enabled": false,
    "breadcrumbs.enabled": false,
    "git.autofetch": true,
    "workbench.startupeditor": "newuntitledfile",
    "explorer.confirmdelete": false,
    "git.enablesmartcommit": true,
    "git.confirmsync": false,
    "window.zoomlevel": 0,
    "explorer.confirmdraganddrop": false
}

我已经将 go111module 环境变量设置为打开,这是 go env 的输出

set GOARCH=amd64
set GOBIN=C:\Users\Francesco\Go\bin
set GOCACHE=C:\Users\Francesco\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Francesco\Go
set GOPROXY=
set GORACE=
set GOROOT=c:\go
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\Francesco\Go\src\test\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\FRANCE~1\AppData\Local\Temp\go-build928398422=/tmp/go-build -gno-record-gcc-switches

我应该改变什么才能使这项工作正常进行?


解决方案


在一些特定情况下,我会遇到这种情况。这是我的故障排除过程:

  1. 您是否运行了 go get github.com/gomodule/redigo/redis

  2. 有时,当我在与项目不同的根目录中打开编辑器时,我会遇到类似的问题。 (另请检查答案末尾的更新)

.  <- editor open here
| 
|_folder
  | main.go
  | go.mod
  | go.sum
  1. 确保您的工具是最新的:运行 ctrl + shift + p,输入 go 并选择 install/update tools

  2. 尝试将您的项目移出 gopath,并为其设置 go.mod。

  3. 重新启动编辑器

针对问题“2”的更新:

go 1.18 有 a new feature 个名为工作空间! 如果您在与 go mod 文件所在的根目录不同的根目录中打开工作区,则可能是因为同一文件夹中有多个项目。如果是这种情况,您可以运行:

go work init
go work use ./path-to-module  ./path-to-module2

它看起来像什么:

.  <- editor open here
| go.work
| app (folder)
  | go.mod
  | go.sum
  | main.go
| client (folder)
  | go.mod
  | go.sum
  | main.go

我在使用不同的软件包(在 mac 上)时遇到了同样的问题,

  1. 更新 go 工具 - ctrl + shift + pcmd + shift + p 并更新/安装 go 工具
  2. 重新启动 vscode

问题已解决

以上就是《VSCode:无法引入 Golang 模块》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>