Integrated developer environments
The Integrated Developer Environments (IDEs) are essentially text editors with additional features that make writing code and building software easier. Text with special meaning, such as string literals, types, function names, and so on are often colored differently by syntax highlighting, or you may get autocomplete options as you're typing. Some editors even point out errors in your code before you've executed it.
There are many options to choose from, and mostly, it comes down to personal preference, but we will look at some of the more popular choices as well as how to set them up to build Go projects.
The most popular editors include the following:
Sublime Text 3
Visual Studio Code
Atom
Vim (with vim-go)
You can see a complete curated list of options at https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins.
In this section, we are going to explore Sublime Text 3 and Visual Studio Code.
Sublime Text 3
Sublime Text 3 is an excellent editor to write Go code that runs on OS X, Linux, and Windows and has an extremely powerful expansion model, which makes it easy to customize and extend. You can download Sublime Text from http://www.sublimetext.com/ and trial-use it for free before deciding whether you want to buy it or not.
Thanks to DisposaBoy (refer to https://github.com/DisposaBoy), there is already a Sublime expansion package for Go, which actually gives us a wealth of features and power that a lot of Go programmers actually miss out on. We are going to install this GoSublime
package and then build upon it to add our desired on-save functionality.
Before we can install GoSublime
, we need to install Package Control into Sublime Text. Head over to https://sublime.wbond.net/ and click on the Installation link for instructions on how to install Package Control. At the time of writing this, it's simply a case of copying the single, albeit long, line command and pasting it into the Sublime console, which can be opened by navigating to View | Show Console from the menu.
Once this is complete, press shift + command + P and type Package Control: Install Package
and press return when you have selected the option. After a short delay (where Package Control is updating its listings), a box will appear, allowing you to search for and install GoSublime just by typing it in, selecting it, and pressing return. If all is well, GoSublime will be installed and writing Go code will just become an order of magnitude easier.
Tip
Now that you have GoSublime installed, you can open a short help file containing the details of the package by pressing command + ., command + 2 (the command key and period at the same time, followed by the command key and number 2).
For some additional help while saving, press command + ., command + 5 to open the GoSublime settings and add the following entry to the object:
"on_save": [ { "cmd": "gs9o_open", "args": { "run": ["sh", "go build . errors && go test -i && go test && go vet && golint"], "focus_view": false } } ]
Tip
Note that the settings file is actually a JSON object, so ensure that you add the on_save
property without corrupting the file. For example, if you have properties before and after, ensure the appropriate commas are in place.
The preceding setting will tell Sublime Text to build the code looking for errors, install test dependencies, run tests, and vet the code whenever we save the file. Save the settings file (don't close it just yet), and let's see this in action.
Navigate to Choose File | Open... from the menu and select a folder to open for now, let's open our tooling
folder. The simple user interface of Sublime Text makes it clear that we only have one file in our project right now: main.go
. Click on the file and add some extra linefeeds, and add and remove some indenting. Then, navigate to File | Save from the menu, or press command + S. Note that the code is immediately cleaned up, and provided that you haven't removed the oddly placed return statement from main.go
, you will notice that the console has appeared and is reporting the issue thanks to go vet:
main.go:8: unreachable code
Holding down command + shift and double-clicking on the unreachable code line in the console will open the file and jump the cursor to the right line in question. You can see how helpful this feature is going to be as you continue to write Go code.
If you add an unwanted import to the file, you will notice that on using on_save
, you are told about the problem, but it wasn't automatically fixed. This is because we have another tweak to make. In the same settings file as the one you added the on_save
property to, add the following property:
"fmt_cmd": ["goimports"]
This tells GoSublime to use the goimports
command instead of go fmt
. Save this file again, and head back to main.go
. Add net/http
to the imports again, remove fmt
import, and save the file. Note that the unused package was removed, and fmt
was put back again.
Visual Studio Code
A surprise entry in the running for best Go IDE is Microsoft's Visual Studio Code, available for free at https://code.visualstudio.com.
Once you've downloaded it from the website, open a Go file (any file with a .go
extension) and note that Visual Studio Code asks whether you'd like to install the recommended plugins to make working with Go files easier:
Click on Show Recommendations and click on Install next to the suggested Go plugin:
It may ask you to restart Visual Studio Code to enable the plugin, and it may also ask you to install some additional commands:
Click on Install All to install all the dependencies, being sure to wait for the previous installation process to finish before initiating others. After a short while, you will notice that a few tools were installed.
Write some messy code (or copy and paste some from https://github.com/matryer/goblueprints/blob/master/appendixA/messycode/main.go) into Visual Studio Code and hit save. You will notice that the imports were fixed and the code was nicely formatted as per the Go standard.
There are many more features that you can make use of, but we won't dig into them further here.