Screen Too Small

This course material requires a larger screen to properly display the terminal and editor interfaces.

Please use a tablet, laptop, or desktop computer for the best learning experience.

You can continue browsing, but the experience will be significantly limited.

25/25
Free Trial: 4 lessons remaining
Unlock Full Access

Debugging Example with Go

Estimated time: 5 minutes

Now I have to admit, I don't have a lot of experience with languages that rely on debuggers (outside of javascript, but that is easily done in the browser and I've never seen anyone hook up a JS debugger to an IDE). Suffice to say, I don't have a lot of experience setting up debuggers in Neovim.

But thankfully, nvim-dap has great information that will help us find debuggers and debug adapters to configure for our Neovim setup. The information is stored in the Debug Adapter Installation Wiki

here is a link: https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation

In this setup, we are going to start with the Go language and its Debug Adapter.

As a quick aside. Debugging in Neovim is very tricky. Mostly because there are many different debuggers and debug adapters. And these tools don't always communicate the same way. So not every language will have the same looking setup. Many languages will use different tools and different configurations to achieve a debugging setup in Neovim. It's hard to get going, but with the Wiki, and nvim-dap we can figure it out!

Our Go Debugger: Delve

With Go, we are going to want to use delve. delve is a debugger that has a debug adapter built in. Having a debug adapter built in will simplify our setup here.

You can install delve with brew, linux package manager, and probably even Mason. In this example we're going to install with brew. brew install delve.

Next, we will have to make sure the delve executable is in your path. If you don't know how to do this, check out "setting up your gopath": https://go.dev/wiki/SettingGOPATH

Now, install the Neovim tooling for Go

So we have our debugger, with delve and we have our dap and dap-ui in Neovim. Now all we need is the actual debug adapter that allows Neovim to talk to delve. In this case, we will use nvim-dap-go.

here is the repo for nvim-dap-go: https://github.com/leoluz/nvim-dap-go

To install, we want to add leoluz/nvim-dap-go to our dependency table in our configuration file for debugging.lua. We then want to initialize nvim-dap-go like this: lua require('dap-go').setup().

Here is what our final debugging.lua file looks like:

return {
    "mfussenegger/nvim-dap",
    dependencies = {
        "leoluz/nvim-dap-go",
        "rcarriga/nvim-dap-ui",
    },
    config = function()
    require("dapui").setup()
        require("dap-go").setup()

        local dap, dapui = require("dap"), require("dapui")

        dap.listeners.before.attach.dapui_config = function()
            dapui.open()
        end
        dap.listeners.before.launch.dapui_config = function()
            dapui.open()
        end
        dap.listeners.before.event_terminated.dapui_config = function()
            dapui.close()
        end
        dap.listeners.before.event_exited.dapui_config = function()
            dapui.close()
        end

        vim.keymap.set("n", "<Leader>dt", ":DapToggleBreakpoint<CR>")
        vim.keymap.set("n", "<Leader>dc", ":DapContinue<CR>")
        vim.keymap.set("n", "<Leader>dx", ":DapTerminate<CR>")
        vim.keymap.set("n", "<Leader>do", ":DapStepOver<CR>")
    end,
}

And we can see our debugger in action!

When the above steps are completed. We should be able to set breakpoints for our go application. When we run our application, we can then see our debugger in action. Check out the video below:

You've completed this lesson!

You completed this lesson less than a minute ago.