Golang Tools Vim

Go Debug VIM: Use VIM to debug Golang (demo video)

Debugging Go code from within VIM using the excellent vim-go plugin.

Debug Go Code from VIM

Debug Go Code – that was the one last thing that I was doing outside of VIM/Terminal was debugging the code. Debugging the compiled code, I must say. Because, for Python as an example, it’s a bit different – you could useĀ pudb.

In the case of Go codebase, Goland – the IDE that I was mostly using – providing a great experience, easy to debug, everything is in the visibility area, you can execute a statement and see the result.

That all is not something you give away easily, but my desire to avoid any additional tool and stick to the terminal window for everything I need for development was much higher. So I decided to move.

List of used additianl plugins looks like this:

Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
Plug 'scrooloose/nerdtree'
Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
Plug 'vim-airline/vim-airline'
Plug 'scrooloose/nerdtree'
Plug 'Valloric/YouCompleteMe'
Plug 'ryanoasis/vim-devicons'
Plug 'easymotion/vim-easymotion'
Plug 'mileszs/ack.vim'
Plug 'airblade/vim-gitgutter'

The main plugin for Go is vim-go.

vim-go's logo from the plugins GitHub page
From vim-go’s GitHub page.

This plugin adds support for the Go language. Additional to all these I also configured the following keyboard mappings:

:nnoremap <leader>b :GoDebugBreakpoint<CR>
:nnoremap <leader>n :GoDebugContinue<CR>

With these two, it is more convenient to set a breakpoint and continue debugging when needed. Of course, these commands can be executed via the ex command.

Here is the short video showing the process:

While in a debug mode, depending on the context, there are also commands like :GoDebugStep, :GoDebugNext.

For more detailed information, please refer to :help vim-go. It really has very extensive documentation.

Starting the debugger

There is one nuance of the vim-go that I missed in the beginning. I was debugging the command line application, which expects input in the startup. Let’s say something like this:

$ webtext

If the input is required and you start the app by just running :GoDebugStart obviously you will not get this input. The fix is quite straightforward:

:GoDebugStart .

As the documentation mentions:

Use :GoDebugStart or :GoDebugTest to start the debugger. The first
argument is the package name, and any arguments after that will be passed on
to the program; for example:

:GoDebugStart . -someflag value

By Kanan Rahimov

Sr. Software Engineer

Leave a Reply