Skip to content

DAP support go

Zeioth edited this page Aug 26, 2023 · 8 revisions

Nothing special needs to be done for Go.

How to setup DAP to debug with Go

Please note that this section has nothing to do with compiler.nvim. I'm documenting this to make your life easier. To debug Go with DAP you have to:

  • Install the mason package delve.
  • Have initialized your module with go mod init module_name.
  • :cd your project before running DAP.

Here you have an example of how to configure DAP for go

local dap = require("dap")

-- Go
-- Requires:
-- * You have initialized your module with 'go mod init module_name'.
-- * You :cd your project before running DAP.
dap.adapters.delve = {
  type = 'server',
  port = '${port}',
  executable = {
    command = vim.fn.stdpath('data')..'/mason/packages/delve/dlv',
    args = {'dap', '-l', '127.0.0.1:${port}'},
  }
}
dap.configurations.go = {
  {
    type = "delve",
    name = "Compile module and debug this file",
    request = "launch",
    program = "./${relativeFileDirname}",
  },
  {
    type = "delve",
    name = "Compile module and debug this file (test)",
    request = "launch",
    mode = "test",
    program = "./${relativeFileDirname}"
  },
}

How to check if everything works

Just insert a breakpoint, run :DapContinue and you will see something like

screenshot_2023-08-10_00-42-37_531868897

Congratulations, now you can compile and debug Go.

Important for you to know

  • You don't need any mason package.
  • You don't need any plugin.
  • You don't need to manually compile the program in debug mode before calling DAP. dlv do it automatically for you.
  • If you find any issue while configuring DAP, run :DapSetLogLevel trace and :DapShowLog to find the reason.