Visual Studio For Golang

Go for Visual Studio Code The VS Code Go extension provides rich language support for the Go programming language. đź“Ł Debugging using Delve 's native DAP implementation is now available for use. Please test it by following the instruction to enable this new feature and share your feedback!

  1. Getting started with Go on Visual Studio Code. Education Details: Jun 25, 2018 The Go extension for Visual Studio Code makes use of a number of Go command line tools. They are not installed with the extension, but there is an installation process that provides a convenient way of adding them to the configuration.
  2. Download and install VSCode from the following link. Download Visual Studio Code - Mac, Linux, Windows. After the installation, Launch VSCode. Open the Extensions Marketplace ( Cmd+Shift+X ), search Go and install it. Open the Command Palette ( Cmd+Shift+P) and run the Go: Install/Update Tools command. Install all the Go extensions listed there.
Visual

I’ve been using VS Code for my Golang development needs for a few months now. Minor kinks here and there, nothing serious, and the development experience gets better with every update. I have also tried out IntelliJ Idea as the editor, and one feature that I’m missing in Code from Idea is the build-run-reload process. I thought to myself, that’s such a basic feature, it should be possible to have that.

And it is! VS Code Tasks to the rescue!

These tasks allow us to run different kind of tools and, well, tasks inside VS Code.

Go to Tasks -> Configure Default Build Task and then select the “Create tasks.json file from template” in the little pop-up window, and after that select the “Others” option. This tasks.json file will live inside the .vscode directory.

For my overcomplicated d20 roller, which is my first website built with Golang, I have the following content for the tasks:

What this one task does is that it runs go build to build the project and then runs the generated executable, which for this project is d20.

Visual

I guess providing a standardized name to go build with the -o flag this could be made more portable so that the command part reads something like go build -o proj && ./proj, but I’m ok with this for now.

And now just type Ctrl+Shift+b and Code will execute this “Build and run” task for us! Hooray! The terminal window in Code will pop-up saying something like:

By going to http://localhost:8080 I can see that my little website is up and running. Cool.

Visual studio golang autocomplete

But if we want to restart this taks, running Ctrl+Shift+b again won’t work and Code will complain that the “Task is already active blablabla…'.

Looking at the Tasks menu, we can see that there’s a “Restart running task…” menu entry. Clicking that, it pops up a window with a list of running tasks. In this case there’s only one, our “Build and run” task. Clicking through the menu every time would be boring, so let’s add a keyboard shortcut for it.

Go to File -> Preferences -> Keyboard shortcuts (or just hit Ctrl+k Ctrl+s), search for “Restart running task” keybinding, and set it to whatever you like. I’ve set it to Ctrl+Alt+r.

Finally, the flow is Ctrl+Shift+b to start the taks for the very first time, code-code-code, Ctrl+Alt+r to re-build. Sweet. Now the only annoying bit is that I have to pick out that one running task from the list of running tasks when restarting, but I can live with that. For now.

Happy hackin’!

Since Go 1.11, modules are the official way of managing dependencies in Go. The transition is not as smooth as it should be as it is in a very early stage. The feature is planned to be finalized with Go 1.14.

This articles explains the setup and recommends settings for Visual Studio Code.

go.mod basics

A module is a collection of related Go packages that are versioned together. All dependencies and their exact versions of your package imports are stored in the go.mod file.

Visual studio golang tutorial

See this Github page for more in-depth information on modules.

Here is an examplariy go.mod file.

The specified module is my-project/my-repo. The used version of Go is 1.12. Within this package, three imports are used.

There are a few options for getting a specific dependency:

Go module setup in Visual Studio Code

Enable Go modules on your machine with

To enable module support in Visual Studio Code, add the following to your IDE settings (press CTRL+,):

This will trigger VS Code to install the go package gopls (The Go Language Server). Visual Studio uses gopls, an implementation of the Language Server Protocol server for Go. It supports features, such as

  • Autocompletion
  • Jump to definition
  • Signature help
  • Hover
  • Document symbols
  • References
  • Rename

It is currently in alpha state, so it is not stable. To install or update gopls, run

However, you can also use the VSCode command Go: Install/Update Tools to install and update all available Go tools. It is located under “View” -> “Command Palette”.

After enabling this feature, I had several problems to a point where Visual Studio Code was almost not usable anymore (on file save, an old version was loaded and all was lost; go-to-definition not working or very slow, and several other problems).

Therefore, it is recommended to look out for updates of the Go tools. However, the following additional settings in VS Code resolved my problems.

Visual Studio For Golang 2019

Some more settings which I find helpful for my daily work with Go.