I want to use VSCode (1.45.0-insider) with Go and modules. This has been quite the hurdle and a frustrating experience to get it to work, so I will document the process for future reference.

The setup I want to use includes using gvm to manage different Go versions. You can read more about gvm and how to use it here.

The project name to follow along this instructions is go-tnl. All the terminal commands are executed from a Terminal view in VSCode.

gvm

We want to install Go v1.13, we use the -B flag to download the binary and skip the build:

$ gvm install go1.13.3 -B

Once we have it installed we want to set it as our Go version:

$ gvm use go1.13.3

Now we can create a pkgset which is how gvm bounds dependencies to a given project. We want to create a pkgset for our project:

$ gvm pkgset create go-tnl

And now we want to use it:

$ gvm pkgset use go-tnl

The last command should have updated your GOPATH and GOROOT for the current terminal:

$ echo $GOPATH
/Users/goliatone/.gvm/pkgsets/go1.13.3/go-tnl:/Users/goliatone/.gvm/pkgsets/go1.13.3/global
$ echo $GOROOT
/Users/goliatone/.gvm/gos/go1.13.3

VSCode

We want to use VSCode's language server for Go, at the time of writing this is still in alpha and unstable.

Create a directory to store the project settings- if you don't have one already:

$ mkdir -p .vscode

Create a local VSCode settings file with the following content:

{
    "go.lintTool": "golint",
    "go.gopath": "/Users/goliatone/.gvm/pkgsets/go1.13.3/go-tnl:/Users/goliatone/.gvm/pkgsets/go1.13.3/global",
    "go.toolsGopath": "/Users/goliatone/.gvm/pkgsets/go1.13.3/global",
    "go.root": "/Users/goliatone/.gvm/gos/go1.13.3",
    "go.useLanguageServer": true,
    "[go]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true,
        },
        // Optional: Disable snippets, as they conflict with completion ranking.
        "editor.snippetSuggestions": "none",
    },
    "[go.mod]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true,
        },
    },
    "gopls": {
        // Add parameter placeholders when completing a function.
        "usePlaceholders": true,
        // If true, enable additional analyses with staticcheck.
        // Warning: This will significantly increase memory usage.
        "staticcheck": false,
    }
}

You will have to reload the window for the language server and other changes to kick in. You can run it from the command palette:

Ctrl + Shift + P

Then type:

Reload Window

Once it reloads you have to set up your environment in the Terminal window again:

$ gvm use go1.13.3
$ gvm pkgset use go-tnl

Then you should see a message from VSCode telling you that some packages are missing. I had to install those from the terminal window since installing from the command palette used the wrong Go version.

$ go get -v golang.org/x/tools/gopls

And:

go get -v golang.org/x/tools/cmd/goimports

Notes

Initially I was trying to set up the environment using go1.14 but some VSCode dependencies did not support that version, then I tried using go1.12 but then Cobra wouldn't work as expected. I'm sure I could have figured out the right version of packages to import but I also had some issues trying to go get specific versions...

Resources: