【问题标题】:How do I properly use go modules in vscode?如何在 vscode 中正确使用 go 模块?
【发布时间】:2020-01-14 11:13:50
【问题描述】:

我已经在我的 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

我的 go.mod 文件:

module 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
)

当我从 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

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

谁能帮帮我?

谢谢!

最好的问候, 拉法尔。

【问题讨论】:

    标签: visual-studio-code go-modules


    【解决方案1】:

    我意识到如果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
    

    如果从 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
    

    【讨论】:

      【解决方案2】:

      我意识到如果 go.mod 不在你项目的根目录下,VSCode 就不能正常工作

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

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


      正如kayochinthe comments 中所指出的:

      【讨论】:

      • 不得不重新启动 vscode 但它删除了我所有的红色。谢谢
      • 设置应该是"gopls": {"build.experimentalWorkspaceModule": true}。对于那些对完整设置的文档感兴趣的人,这里是链接github.com/golang/tools/blob/master/gopls/doc/…
      • @kayochin 谢谢你的评论。我已将您的答案包含在答案中以提高知名度。
      【解决方案3】:

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

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

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

      "go.useLanguageServer": true

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

      您还应该修复自动导入:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-24
        • 2014-09-17
        • 1970-01-01
        • 2020-09-09
        • 1970-01-01
        • 1970-01-01
        • 2019-10-31
        • 1970-01-01
        相关资源
        最近更新 更多