【问题标题】:Golang VSCode configuration best setupGolang VSCode 配置最佳设置
【发布时间】:2020-03-26 09:03:56
【问题描述】:

我想知道是否有人可以在 Mac 上使用 Vscode 分享他们对 Golang 的最佳设置。由于我不断遇到以下问题:

  1. 有时 gomft 不起作用。
  2. 在同一个包中定义的函数显示带有红色下划线警告,表示它未定义(但在运行时有效)
  3. 弹出以下消息

Your workspace is misconfigured: command-line-arguments has no metadata. Please see https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md for more information or file an issue (https://github.com/golang/go/issues/new) if you believe this is a mistake.

以下是我的 go 设置的一些信息:

  • 版本:go1.13.4 darwin/amd64
  • “settings.json”中的设置:

    {
        "window.zoomLevel": 1,
        "workbench.iconTheme": "material-icon-theme",
        "editor.accessibilitySupport": "off",
        "go.useLanguageServer": true,
        "go.formatTool": "gofmt",
        "go.lintTool": "golangci-lint",
        "[go]": {
            "editor.formatOnSave": true,
            "editor.codeActionsOnSave": {
                "source.organizeImports": true
            },
        },
        "explorer.confirmDragAndDrop": false,
        "javascript.updateImportsOnFileMove.enabled": "always",
        "diffEditor.ignoreTrimWhitespace": false
    }

  • GOPATH 指向安装 Go 的文件夹

【问题讨论】:

  • 这类问题有时也会发生在我身上。重新启动 VS Code 为我解决了问题。

标签: go visual-studio-code installation


【解决方案1】:

你可以试试这个设置。

    "go.inferGopath": false,
    "go.buildOnSave": "workspace",
    "go.lintOnSave": "package",
    "go.vetOnSave": "package",
    "go.buildTags": "",
    "go.buildFlags": [],
    "go.lintFlags": [],
    "go.vetFlags": [],
    "go.coverOnSave": false,
    "go.useCodeSnippetsOnFunctionSuggest": false,
    "go.formatTool": "goreturns",
    "go.gocodeAutoBuild": false,
    "go.useLanguageServer": true,
    "go.alternateTools": {
      "go-langserver": "gopls", 
    },
    "go.languageServerExperimentalFeatures": {
      "format": true,
      "autoComplete": true
    },
    "[go]": {
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        },
    },

【讨论】:

    【解决方案2】:

    go.useLanguageServer: false,可能对你有帮助

    【讨论】:

    • 欢迎来到 SO 并感谢您帮助他人。如果您添加一些额外的解释,您的答案可能会更有帮助。
    • 它似乎适用于代码中标记为红色的区域。 '没有定义的'。但是根据语言服务器上的文档,它也应该带来一些好处,所以通过禁用它,它可能会禁用某些功能? - The Language Server protocol is used between a tool (the client) and a language smartness provider (the server) to integrate features like auto complete, go to definition, find all references and alike into the tool
    • 可能无法使用导航到代码符号的定义。
    【解决方案3】:

    我看到了同样的问题,并且还使用 golangci-lint 作为 lint 工具。当我查看 Go 扩展的输出时(视图 > 命令托盘 > 输出:关注输出视图;然后在输出视图的下拉列表中选择“Go”),我看到如下错误:

    Error while running tool: /usr/local/bin/golangci-lint run --print-issued-lines=false
    level=warning msg="[runner] Can't run linter goanalysis_metalinter: assign: failed prerequisites: inspect@example.com/pkg/example"
    

    还有这个

    level=error msg="Running error: buildssa: analysis skipped: errors in package: [/Users/tschaub/projects/pkg/example.go:6:15: undeclared name: SomethingIJustStartedTyping
    

    第一个错误 (assign: failed prerequisites) 已在 https://github.com/golangci/golangci-lint/issues/827 出票,并以关于更新到 golangci-lint@v1.24.0 的评论结束。

    第二个错误 (buildssa: analysis skipped) 已在 https://github.com/golangci/golangci-lint/issues/896 出票,并且在我键入此内容时正在接收新的 cmets。

    Upgrading to v1.24.0 可能无法解决问题,但听起来它可能会生成更具描述性的输出。

    即使使用golangci-lint@v1.24.0 也不能解决我的问题。每当我进入这种“错误配置”状态时,我都需要退出 VSCode 并重新启动它。我希望在工具赶上 go 模块之后,事情会更加稳定/可靠。

    【讨论】:

    • 我试图回到戈兰,但不再适应了。希望他们尽快解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多