简体   繁体   English

Golang VSCode 配置最佳设置

[英]Golang VSCode configuration best setup

I wonder if someone can share their best setup for Golang using Vscode on Mac.我想知道是否有人可以在 Mac 上使用 Vscode 分享他们对 Golang 的最佳设置。 As I am continously having issues such as:由于我不断遇到问题,例如:

  1. sometimes gomft doesn't work.有时 gomft 不起作用。
  2. Functions defined in the same package shows with a red underline warning saying it's not defined (but works when running it)在同一包中定义的函数显示有红色下划线警告,表示未定义(但在运行时有效)
  3. having the following message popped up弹出以下消息

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.

Here are some information of my go setup:以下是我的 go 设置的一些信息:

  • Version: go1.13.4 darwin/amd64版本:go1.13.4 darwin/amd64
  • Settings in 'settings.json': “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 is pointing to the folder where Go is installed GOPATH 指向安装 Go 的文件夹

you can try this setting.你可以试试这个设置。

    "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
        },
    },

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

I'm seeing the same problem and also using golangci-lint for a lint tool.我看到了同样的问题,并且还使用golangci-lint作为 lint 工具。 When I look at the output from the Go extension (View > Command Pallet > Output: Focus on Output View; then choose "Go" in the dropdown for the output view), I see errors like this:当我查看 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"

And this和这个

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

The first error ( assign: failed prerequisites ) was ticketed in https://github.com/golangci/golangci-lint/issues/827 and closed with a comment about updating to golangci-lint@v1.24.0 .第一个错误( assign: failed prerequisites )在https://github.com/golangci/golangci-lint/issues/827 中被罚单,并以关于更新到golangci-lint@v1.24.0的评论golangci-lint@v1.24.0

The second error ( buildssa: analysis skipped ) was ticketed in https://github.com/golangci/golangci-lint/issues/896 and is receiving new comments as I type this.第二个错误 ( buildssa: analysis skipped ) 在https://github.com/golangci/golangci-lint/issues/896 中被罚单,并在我输入时收到新评论。

Upgrading to v1.24.0 may not solve the issue, but it sounds like it might generate more descriptive output.升级到 v1.24.0可能无法解决问题,但听起来它可能会生成更具描述性的输出。

Even using golangci-lint@v1.24.0 doesn't fix the problem for me.即使使用golangci-lint@v1.24.0也不能解决我的问题。 I need to quit VSCode and restart it whenever I get in this "misconfigured" state.每当我进入这种“错误配置”状态时,我都需要退出 VSCode 并重新启动它。 I'm hoping that after tools catch up with go modules, things will be a bit more stable/reliable.我希望在工具赶上 go 模块之后,事情会更加稳定/可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM