如何在 vscode 中正确使用 go 模块?

Raf*_*cki 6 visual-studio-code go-modules

我在我的 mac 上使用 vscode 1.41.1 几个月了,它运行良好,直到我开始使用 go 模块进行依赖管理。目前,我正在重写一个简单的工具,并为不同的功能引入包。

我的代码结构如下所示:

??? bmr.go -> package main & main(), uses below packages
??? check
?   ??? check.go -> package check
?   ??? check_test.go
??? go.mod
??? go.sum
??? push
?   ??? push.go -> package push
?   ??? push_test.go
??? s3objects
    ??? s3objects.go -> package s3objects
    ??? s3objects_test.go
Run Code Online (Sandbox Code Playgroud)

我的 go.mod 文件如下所示:

模块 github.com/some-org/business-metrics-restore

go 1.13

require (
        github.com/aws/aws-sdk-go v1.28.1
        github.com/go-redis/redis v6.15.6+incompatible
        github.com/sirupsen/logrus v1.4.2
        github.com/spf13/viper v1.6.1
        github.com/stretchr/testify v1.4.0
        golang.org/x/sys v0.0.0-20200113162924-86b910548bc1
)
Run Code Online (Sandbox Code Playgroud)

当我从 shell 调用 go test/run/build 命令时,一切都很好。但是当我使用 'Debug' -> 'Run without Debugging' 我得到:

go: finding github.com/some-org/business-metrics-restore/push latest
go: finding github.com/some-org/business-metrics-restore latest
go: finding github.com/some-org/business-metrics-restore/check latest
go: finding github.com/some-org/business-metrics-restore/s3objects latest
build command-line-arguments: cannot load github.com/some-org/business-metrics-restore/check: module github.com/some-org/business-metrics-restore@latest found (v0.0.0-20191022092726-d1a52439dad8), but does not contain package github.com/some-org/business-metrics-restore/check
Process exiting with code: 1
Run Code Online (Sandbox Code Playgroud)

我的代码目前在一个功能分支中,d1a52439dad8 是第一个(init)并且只在 master 上提交。主分支中没有该工具的代码(包括 3 个提到的非主包)。这里的问题是,由于某种原因,如您在上面看到的,vscode 从 master 获取状态,而我无法覆盖此行为。

谁能帮我?

谢谢!

最好的问候,拉法尔。

小智 15

我意识到如果go.mod不在你项目的根目录下,VSCode 就不能正常工作。我有一个具有以下结构的 AWS SAM 项目:

??? Makefile
??? README.md
??? nic-update
?   ??? go.mod
?   ??? go.sum
?   ??? main.go
?   ??? main_test.go
?   ??? r53service
?       ??? r53.go
??? samconfig.toml
??? template.yaml
Run Code Online (Sandbox Code Playgroud)

并且它的唯一工作方式是从nic-update目录中启动 VSCode 。

我的 go.mod 有以下内容:

require (
    github.com/aws/aws-lambda-go v1.13.3
    github.com/aws/aws-sdk-go v1.32.12
)

module github.com/jschwindt/ddns-route53/nic-update

go 1.14
Run Code Online (Sandbox Code Playgroud)


Von*_*onC 9

我意识到如果 go.mod 不在项目的根目录下,VSCode 将无法正常工作

现在(2020 年 10 月)可能会得到支持,因为gopls v0.5.1及其来自提案 32394 的实验性功能多模块工作区支持。

即使您没有多个模块,go.mod子文件夹(而不是项目的根文件夹)中的 a 也会得到更好的管理(如果您激活gopls.experimentalWorkspaceModule设置)。

  • 不得不重新启动 vscode 但它删除了我所有的红色。谢谢 (2认同)

Gre*_*ray 6

我也遇到了 VS Code 和模块的问题。Go Modules 的 VS Code 支持的当前状态在此处保持最新状态:https : //github.com/golang/vscode-go#Set-up-your-environment

在该链接中,他们建议放弃 VS Code 鼓励您使用 Go 安装的大多数现有扩展,而是使用具有以下说明的语言服务器 gopls:

在您的设置中添加以下内容以使用它。

“go.useLanguageServer”:真

注意:当 Go 工具团队将新版本标记为稳定版本时,系统将提示您安装最新的稳定版本的 gopls。

您还应该修复自动导入:

添加设置 "go.formatTool": "goimports" 然后使用 Go: Install/Update Tools 安装/更新 goimports,因为它最近添加了对模块的支持。

当您执行这些操作时,请记住,您还将失去一些功能:

  • 完成未导入的包不起作用

  • 查找引用和重命名仅适用于单个包