go - 使用模块时在工作区中制作 VSCode 安装包

标签 go visual-studio-code vscode-settings

我在不使用 go 模块的情况下构建了一个 go 应用程序。

我使用 VSCode 作为编辑器。我在 VSCode 工作区中设置了以下内容

myapp-workspace.workspace-code

{
  "folders": [
    {
      "path": "."
    }
  ],
  "settings": {
    "go.gopath": "/Users/blah/blah/myapp/",
    "go.testFlags": ["-v"]
  }
}

一切正常,我可以安装当前包从 VSCode 命令行中,结果是查看文件 我的应用程序.a 在文件夹 /Users/blah/blah/myapp/bin/myapp .

然后我决定使用转到模块 .我运行命令 go mod init github.com/myuser/myapp ,我的生活不再快乐。

如果我尝试运行 安装当前包我收到错误 Installation failed: $GOPATH/go.mod exists but should not .如果我从工作区中删除设置,则会安装包,但它安装在 中~/go/bin .

配置 VSCode 以便它可以使用模块在我的工作区中安装包的方法是什么?

最佳答案

您的 go.gopath工作区设置(相当于 $GOPATH 用于打开的工作区)指向(我猜)你也有 go.mod 的项目目录.

使用 Go 模块,你不能拥有 go.gopath ( $GOPATH ) 指向使用模块的项目目录。如果你想使用 go getgo install你需要搬家go.gopath其他地方(如果您希望每个项目有一个 GOPATH,它也可以是项目目录中的子目录)。注意可能仍然会导致问题,如 some tools Visual Studio Code uses are module aware and some not .

使用 Go 模块时推荐的解决方案是切换到新的语言服务器 gopls这是模块感知的。这是跟踪 Visual Studio Code 中 Go 模块支持的页面,并指导如何启用 gopls : https://github.com/Microsoft/vscode-go/wiki/Go-modules-support-in-Visual-Studio-Code

关于go - 使用模块时在工作区中制作 VSCode 安装包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61558445/

相关文章:

angular - OPTIONS 请求的 CORS 问题

visual-studio-code - 我怎样才能让 vscode 知道 SCM 何时可见?

go - 如何修复 VS Code 错误 : "Not able to determine import path of current package by using cwd" for Go project

windows - 如何使用 launch.json 定位平台

bash - 无法启动 git bash

golang比较两个实现不同的结构

html - Revel 模板多个变量,在其他范围内访问一个

macos - 我可以提交带有 Mac App Store 应用程序的 Go 程序吗?

visual-studio-code - 如何从 VS Code 的集成终端复制文本?

visual-studio-code - 如何在 Visual Studio Code 中重置符号查找?