Setting up VS Code for Golang (2021)
4 min readNov 30, 2021
Love VS Code and is looking for how to set it up so you can start hacking with Go? Look no further! (ok ok, probably not 100% accurate, but, at least, it’ll put you in the right direction ^^).
Starting with the basics
- Get VS Code https://code.visualstudio.com/learn
- Get Go https://go.dev/learn/
- Install VS Code Go extension https://marketplace.visualstudio.com/items?itemName=golang.go
- Or just search for “Go” in the Extensions tab (
Shift+Ctrl+X
) and install it from there:
- Install or update Go Tools by pressing
Ctrl+Shift+P
, then type in “Go” and select “Go: Install/Update Tools”:
Next steps
Up until this point we’ve covered the minimum to get you up and running. Now it’s time to go a bit further.
- Chances are that you’re already familiar with VS Code Customization features, specially when it comes to themes. So, here’s a favorite choice of mine: https://marketplace.visualstudio.com/items?itemName=teabyii.ayu
- Or just search for “Ayu” in the Extensions tab:
- And here’s a comparison between the default dark theme and Ayu:
- Next customization I really like is changing the font used by the text editor. Here’s a selection of great fonts for coding: https://braydoncoyer.dev/blog/6-best-fonts-for-programming-in-2021/
- And my personal choice is: https://www.jetbrains.com/lp/mono/
- So after choosing and installing your preferred font, just tell VS Code to use it (you can create a
settings.json
file and put it inside a.vscode
folder in the root of your project or workspace):
- In the example above, font ligatures are enabled, so operators like
==
,!=
,:=
,>=
and so on are replaced with their corresponding ligatures:
- Moving on, another really cool feature you could turn on is:
- It’ll enable the use of semantic tokens provided by
gopls
(the Go Language Server) which will improve syntax highlighting defined by Ayu theme:
- Notice how the module’s name is highlighted in blue in the imports section, or how function parameters are painted in purple. It might not look much, but it’s a step above the regular syntax highlighting based on TextMate rules (more on this here https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide and here https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide)
- The last thing I’ve changed is related to how long lines of code are formatted. For those used to the “Format Document” feature (
Shift+Alt+F
), by default, it won’t shorten long lines of code. So, to address this gap I’m usinggolines
(https://github.com/segmentio/golines) in combination with “Run on Save” extension (https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave). Just edit yoursettings.json
and add the following configurations:
Wait, there’s more …
As a bonus, take a look at these last tips:
- If you’re willing to learn more about how to better organize your project and what architecture to use, here’s a great article series to dig in, https://threedots.tech/
- And best part is that there’s a real repository to follow along, so you can check how all the concepts mentioned by them play together: https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example
- At last, did you notice how cool the prior code snippets look? There’s a VS Code extension for that: https://marketplace.visualstudio.com/items?itemName=moyu.snapcode