简体   繁体   中英

Golang VSCode configuration best setup

I wonder if someone can share their best setup for Golang using Vscode on Mac. As I am continously having issues such as:

  1. sometimes gomft doesn't work.
  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:

  • Version: go1.13.4 darwin/amd64
  • Settings in '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

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. 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:

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 .

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.

Upgrading to v1.24.0 may not solve the issue, but it sounds like it might generate more descriptive output.

Even using golangci-lint@v1.24.0 doesn't fix the problem for me. I need to quit VSCode and restart it whenever I get in this "misconfigured" state. I'm hoping that after tools catch up with go modules, things will be a bit more stable/reliable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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