Go is a fantastic programming language. It also comes with a comprehensive tool chain. Many developers just use plain text editors with or without plugins to write Go code, but many others prefer to use proper IDEs (integrated development environments).
In this tutorial, you'll learn about the current state of Go IDEs.
Why Use an IDE?
I was always a big fan of IDEs. Early in my career, I used Microsoft's Visual Studio for C/C++ development as well C#. I've used NetBeans for a short stint of Java development. I've used JetBrains' PyCharm for Python development, and nowadays I use JetBrains' GoLand for Go development.
I typically work on large-scale software systems that consist of multiple projects with thousands of files. I need a lot of help with large codebases. Here the main reasons why I like IDEs so much:
Integrated Code Organization and Navigation
With a good IDE, you can organize your code hierarchically, view and navigate it with ease, and also quickly search your code. The IDE will do much more than just text search and will let you filter based on language-specific constructs (e.g. show only classes or interfaces with a particular name).
Integrated testing is also crucial. I spend most of my development time on tests—either writing them, trying to make them pass, or investigating why tests suddenly failed. A good IDE with integrated testing will let you run tests in a selective way (all, just one project, just failed tests from previous run), will highlight failures, and will let you jump quickly to the right place in the code to fix any problem.
This is probably the most important feature for me. I love integrated debuggers. I want to be able to add a breakpoint (including conditional breakpoints), view the stack trace, and view the variables at each frame.
For bonus points (thanks PyCharm), I like to have an interactive console session when a breakpoint hits. The alternatives of printf debugging (requires modifying the code and rebuilding) or viewing log files (often not enough granularity or a lot of noise) are much more frustrating and less productive. Even standalone debuggers like gdb don't come close to the productivity of a good integrated debugger.
GoLand is my current Golang IDE. It is the most popular IDE for Go programming and offers a lot of functionalities and plugins. It is built on the strong foundation of JetBrains, so it's incredibly robust and feature-full.
Code Editor and Assistance
GoLand's code editor is superb. Here are some of its capabilities:
- tabbed interface
- smart code completion
- inspections and quick fixes
- quick navigation
- quick popups for documentation, definition, usages, structure, etc.
- code generation (eg. implement interface)
- detecting recursive calls
- showing the type of any expression
- highlighting function exit points
- parameter hints
- built-in formatter
The GoLand debugger is excellent. It is built on top of Delve, which is used by other IDEs we cover too. It uses the familiar JetBrains debug configurations to control everything about a debug run such as the executed binary, the environment variables, the working directory, and the command-line arguments.
You can set breakpoints, conditional breakpoints, and watches. When a breakpoint is hit, you can observe the stack trace and the local variables in each frame. Unfortunately, global variables are not displayed and are often necessary in Go.
I had one issue with the debugger where breakpoints are hit prematurely! Consider the following Go snippet:
x := 1
y := 3
z := x + y
Sometimes, if I put a breakpoint on the last line, it would hit, but the value of z would be 0, instead of 4. How can it be? It turns out that the debugger got confused somehow. If I keep stepping through the code, it will jump back to
z := x + y, assign 4 to z, and then go again to the last line and properly print 4 to the screen.
I wasted a couple of hours trying to debug a complicated piece of code, not realizing I wasn't always getting the correct picture. Eventually, I figured out what was going on and just ignored the fake early triggering of breakpoints.
To solve this issue, you should disable optimizations while debugging.
The test runner lets you run, stop and restart tests. I was able to use it successfully for running and debugging unit tests, as well as integration tests using both Go's testing package as well as a BDD testing framework like ginkgo.
Go comes with various tools, and GoLand integrates them into the IDE. You can invoke any tool from the "Code > Go Tools" menu. The built-in code formatting of GoLand utilizes the standard "go fmt" tool. You can also integrate tools such as 'go vet,' 'go test,' 'go run,' 'go build,' and 'go install' to provide an excellent coding experience.
GoLand inherits the mature source control management system of other JetBrains products. I've used primarily the git support, which allows managing of multiple projects, excellent display of branches, multiple change sets, and more. Other source control systems like Mercurial and SVN are supported too, via additional plugins.
GoLand provides a built-in terminal in case you need to run some commands. It's nice to stay within the IDE and be able to look at or copy from/to editor panes without switching windows completely.
GoLand comes with tons of built-in capabilities, but it is fully extensible, and many of its built-in features are actually pre-loaded plugins. You can install many useful plugins from the GoLand repository, and you can develop your own plugins if you wish.
Visual Studio Code
Visual Studio Code is an extensible open-source code editor developed primarily by Microsoft (with lots of contributions from the community). It is based on Electron, which is in turn based on Chromium. Visual Studio Code supports many languages and originally focused on web development. It has strong support for Go development, including integration of all the Go tools and the Delve debugger via a dedicated extension. You'll need to install some Go packages and tools to get started.
Visual Studio Code also offers built-in git integration, hierarchical folder/file explorer, and a tabbed interface.
The IntelliSense support (autocompletion, showing parameter types and documentation) is great and makes for a very pleasant editing experience. The integrated debugger (also uses Delve under the hood) is very well done and matches the GoLand debugger.
Visual Studio Code is very responsive and quick. Here is its substantial feature list:
- completion lists (using gocode)
- signature help (using gogetdoc or godef+godoc)
- quick info (using gogetdoc or godef+godoc)
- goto definition (using gogetdoc or godef+godoc)
- find references (using guru)
- references CodeLens
- file outline (using go-outline)
- workspace symbol search (using go-symbols)
- rename (using gorename) Note: For undo after rename to work in Windows, you need to have the diff tool in your path.
- build-on-save (using go build and go test)
- lint-on-save (using golint or gometalinter)
- format (using goreturns or goimports or gofmt)
- generate unit tests skeleton (using gotests)
- add imports (using gopkgs)
- add/remove tags on struct fields (using gomodifytags)
- semantic/syntactic error reporting as you type (using gotype-live)
- run tests under the cursor, in current file, in current package, in the whole workspace (using go test)
LiteIDE is a free and open-source IDE for Go Programming, which provides a feature-rich set of tools for Go development. It provides syntax highlighting, code completion, and code navigation, and it also supports Git and debugger integration.
- system environment management
- configurable build commands
- simple and open debug system (supports both gdb and Delve)
- Kate format for auto-completion and theming
- configurable auto-completion with WordApi
- MIME type based system
- plugin support
- quick open
- package browser
- class view and outline
- document browser
- Gocode support
- GOPATH API index
- code navigation
- find usages
- code Refactor
- Go playground
- compile and test support
- doc search and api index
- debugging with GDB and Delve
- golang present
Go Plugins for Other IDEs
There are many general-purpose IDEs that support many programming languages and have added support for the Go language as well. I'm not a big fan of such environments. I believe that an integrated environment must have deeply integrated language-specific hooks. It is difficult to implement as a simple plugin (although you may argue that this is exactly what VS Code does).
Some popular development environments that have added Go support are:
Go is a simple language. A lot of people feel very productive just using a text editor, possibly with some additional Go plugins. I don't consider those really integrated development environments. But it makes sense if you have a lot of years of experience with your text editor, you've already customized it to your liking, and you use it for many other tasks. Here is a list of some of the more popular text editors and their Go plugins:
Emacs has the following plugins for Go:
- GoFlyMake: Flymake-style syntax checking for Go
- go-errcheck.el: Errcheck integration for Emacs
- gopls: The official Go language server provides the sublime text editor IDE features. Its features include autocompletion, refactoring support, and code formatting.
- GoSublime plugin
These have a plethora of Go plugins:
Go has come a long way and has a robust ecosystem that includes many options for the development environment. The Delve debugger provides a fantastic interactive debugging experience and is used by most top IDEs. Play around with these IDEs and find the one that suits you the most.
The right IDE can make working with Go even more fun.