VSCode Settings for Golang through GVM

For you who have been working intensively for developing NodeJs backend apps must be familiar with NVM (Node Version Manager), the tool that manages different version of NodeJs, so you can easily switch back and forth between them. This also what I need when working with Golang project.

After searching and trying, I found GVM (Golang version Manager).

At the beginning, it is kinda trivial for me to setup such an integration with VSCode using GVM, because I think it'll be easy as usual, you just have to install the golang extension and voila it's done.

Golang Extension for VSCode

Turns out it's not like that...

You have to manually setup the configuration because GVM manages the Golang binary itself on the specific folder on $HOME/.gvm, not at the actual place such as /usr/local/bin or similar (you got it unix people!).

After some findings and consulting with stackoverflow, I can set the binary for specific Golang version through the settings.json, which actually dissapointing because I was expected that thing can be done automatically (First Class GVM please!).

GVM Install

GVM is so easy to install, just go to the repository and follows the instructions

then you have to make sure the go version you want is already installed with this command.

$ gvm list

gvm gos (installed)

   go1.16.15
   go1.18.1
=> go1.19.1
gvm list

The command above shows the installed golang version in my computer, the default version is currently pointed at 1.19.1

If you are at the beginning for GVM, just go install by following this section or doing some command executions like below.

$ gvm listall ~> this will show all available go version to instal
$ gvm install go1.19.1 ~> this will install that version
For You who using ARM based CPU, not all versions are available. You must find out what version is supported by crawling at the go.dev download page. (*-arm64)

VSCode Settings

Then open you VSCode user settings via <command> + P and type >

or depending on your platform:

  • Windows %APPDATA%\Code\User\settings.json
  • macOS $HOME/Library/Application\ Support/Code/User/settings.json
  • Linux $HOME/.config/Code/User/settings.json
Because I want to set golang version to global, you can choose a workspace or folder settings if wish a different version according to the project you work on.
Open User Settings JSON

then set this config, according to what golang version you want to use.

{
    "go.gopath": "/Users/kresna/.gvm/pkgsets/go1.19.1/global",
    "go.goroot": "/Users/kresna/.gvm/gos/go1.19.1",
}
go binary path setting to GVM folder

After that, you'll found the vscode confirm the configuration.

VSCode Confirmation

If this is the first time you setup the config for that golang version, the golang extension will also install some tools like gotests gopls staticcheck etc.

Go extension install the tools needed automatically

Then you're good to go, have fun!