Skip to content

DAP support go

Zeioth edited this page Aug 9, 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:

  • Have the system dependency dlv.
  • 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 the system dependency 'dlv'.
-- * You have initialized your module with 'go mod init module_name'.
-- * You :cd your project before running DAP.
-- note that no mason package or nvim plugin is required.
dap.adapters.delve = {
  type = 'server',
  port = '${port}',
  executable = {
    command = '/usr/bin/dlv', -- Ensure this is correct
    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

You don't need to manually compile the program before calling DAP. dlv will do it automatically.

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.
  • If you find any issue while configuring DAP, run :DapSetLogLevel trace and :DapShowLog to find the reason.