Taming the Visual Studio Code for Golang

There are few tips which I have collected over time when I was using Visual Studio Code for editing my Go code.

Using gogetdoc as the default documentation provider

Sometimes I came across a code which wasnt showing tooltips and over a function for me. To me it looks like godoc doesn’t handle well non local sources.

Luckily we can enable gogetdoc quite easily, just open Visual Studio Code, press Shift+Cmd+P (Shift+Alt+P) to open Command Palette and type in Open Settings (JSON), after hitting enter you will get VS’s settings.json opened. Find out if you have go.docsTool set and change it to gogetdoc like this:

"go.docsTool": "gogetdoc",
Screenshot 2019-05-27 at 20.48.29
Your tooltips should now work well.

Speed up the search in folder by keeping file cache alive

By default, simple search in workspace/folder is quite slow. It looks like editor is starting a special process just for search every time you start a new search. It is possible to keep the process in memory which makes search pretty fast. Open the Settings.json as in previous tip and set up:

"search.maintainFileSearchCache": true,

You can compare how fast it is when turned off. The keyboard shortcut to search is Shift+Cmd+F.

 

Enlarge the debugging tooltips for variable content

While debugging under Delve, the default limit used in VS is 64 characters, which is quite not enough. If you dont want to play with slices in debug window which also works.

Evaluating myvar[64:128] prints it

Without changes it looks somethings like this:

debugging-long-text.pngBut fortunatelly it is possible to edit your delve source code to increase limit as you want.

In the settings.json, place following configuration snippet

    "go.delveConfig": {
        "dlvLoadConfig": {
            "followPointers": true,
            "maxVariableRecurse": 1,
            "maxStringLen": 1000,
            "maxArrayValues": 1000,
            "maxStructFields": -1
        },
        "apiVersion": 2,
        "showGlobalVariables": true
    }
It increases the length of text nicely.
Btw. Did you know that there is Game of Thrones Lorem ipsum generator, for example here, the one by Rich Finelli.
The result can be observed here:
Screenshot 2019-05-28 at 20.21.08.png

Enabling the golang test log messages

Similarly like when running go test ./… any log messages produced by your tests like:

    t.Logf("logf Score %v\n", score)
    fmt.Printf("fmtf Score %v\n", score)

Are being ignored unless we start them with go test -v ./…
From those, fmt.Printf is printing immediatelly, while t.Logf waits until the test completes, so sometimes it might be more usefull to use fmt.Printf in tests.

Fortunatelly editor allows us to set up tests to use this argument. Open Settings.json again and write down:

"go.testFlags": ["-v"],
That will show the log messages the the Output window just right.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s